<!-- 任务详情页面 童晓白 2025-07-28 -->
<template>
  <scroll-view 
    scroll-y="true" 
    :refresher-enabled="true"
    :refresher-triggered="refreshing"
    :scroll-into-view="scrollIntoView"
    @refresherrefresh="onRefresh"
    class="scroll-container"
  >
    <view v-if="loading" class="loading"><text>加载中...</text></view>
    <view v-else-if="error" class="error"><text>{{ error }}</text></view>
    <view v-else>
      <!-- 头部卡片 -->
      <view class="task-header">
        <image :src="getAvatarUrl(task?.userAvatar)" mode="aspectFill" class="user-avatar" @error="handleTaskAvatarError" />
        <view class="header-main">
          <view class="header-title-row">
            <text class="task-title">{{ taskData?.title }}</text>
            <view class="status-tag"><text>{{ getStatusText(taskData?.status) }}</text></view>
          </view>
          <view class="task-publisher">发布者：{{ taskData?.userName || '未知用户' }} ({{ taskData?.orgName || '未知企业' }})</view>
          <view class="task-time">发布时间：{{ taskData?.publishTime }}</view>
          <view class="task-deadline">截止日期：{{ taskData?.deadline }}</view>
        </view>
      </view>
      <view class="task-body">
        <view class="task-info-row">
          <text class="task-info-label">任务描述:</text>
          <text class="task-info-value">{{ taskData?.description }}</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">悬赏金额:</text>
          <text class="task-info-value">{{ formatNumber(taskData?.reward) }} 元</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">当前支付:</text>
          <text class="task-info-value">{{ formatNumber(taskData?.currentPaid) }} 元</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">任务状态:</text>
          <text class="task-info-value">{{ getStatusText(taskData?.status) }}</text>
        </view>
        <view v-if="taskData?.challengerId && (taskData?.status === '2' || taskData?.status === '3')" class="task-info-row">
          <text class="task-info-label">挑战者:</text>
          <text class="task-info-value">UID {{ formatNumber(taskData?.challengerId) }}</text>
        </view>
      </view>
      <!-- 进行中时的快捷操作条 -->
      <view v-if="showApplyCompleteBtn || isPublisherAndInProgress" class="quick-actions">
        <button v-if="showApplyCompleteBtn" class="apply-shortcut-btn" @click="goToApplyComplete"><text>去申请完成</text></button>
        <button v-if="isPublisherAndInProgress" class="apply-shortcut-btn" @click="viewApplyCompleteInfo"><text>查看申请完成详情</text></button>
      </view>

      <!-- 任务操作按钮区域 -->
      <view v-if="showTaskActions" class="task-actions">
        <!-- 撤回申请按钮 - 只有申请者可以撤回，且没有待处理的消息 -->
        <button v-if="canCancelApply && !hasPendingMessages" class="cancel-apply-btn" @click="cancelTaskApply">
          <text>撤回申请</text>
        </button>
        
        <!-- 申请取消任务按钮 - 只有进行中任务可以申请取消，且没有待处理的消息 -->
        <button v-if="canRequestCancel && !hasPendingMessages" class="request-cancel-btn" @click="openCancelRequestModal">
          <text>申请取消任务</text>
        </button>
        
        <!-- 删除任务按钮 - 只有发布者可以删除，且任务状态为0或1 -->
        <button v-if="canDeleteTask" class="delete-task-btn" @click="deleteTask">
          <text>删除任务</text>
        </button>
        
        <!-- 待处理消息提示 -->
        <view v-if="hasPendingMessages" class="pending-messages">
          <text class="pending-title">有待处理的消息</text>
          <view v-for="msgType in pendingMessageTypes" :key="msgType" class="pending-item">
            <text v-if="msgType === 3">• 任务申请待处理</text>
            <text v-else-if="msgType === 9">• 取消申请待处理</text>
            <text v-else-if="msgType === 4">• 完成申请待处理</text>
          </view>
          <button class="view-messages-btn" @click="goToMessages">
            <text>查看消息</text>
          </button>
        </view>
      </view>

      <!-- 工作记录（在开始/结束按钮之上显示） -->
      <view v-if="showWorkControls" class="work-records">
        <text class="work-section-title">我的工作记录</text>
        <view v-if="workLoading" class="loading"><text>加载中...</text></view>
        <view v-else-if="workError" class="error"><text>{{ workError }}</text></view>
        <view v-else>
          <view v-if="!workTimes || workTimes.length === 0" class="empty-records"><text>暂无记录</text></view>
          <view v-else>
            <view v-for="wt in workTimes" :key="wt.id || wt.workTimeId" class="work-item">
              <view class="work-item-row">
                <text class="work-item-label">开始</text>
                <text class="work-item-value">{{ formatDateTime(wt.startTime || wt.start_at || wt.start) }}</text>
              </view>
              <view class="work-item-row">
                <text class="work-item-label">结束</text>
                <text class="work-item-value">{{ (wt.endTime || wt.end_at || wt.end) ? formatDateTime(wt.endTime || wt.end_at || wt.end) : '进行中' }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 开始/结束工作按钮（在申请完成之上显示） -->
      <view v-if="showWorkControls" class="work-actions">
        <button 
          class="start-work-btn" 
          :disabled="hasOngoingWork || startWorking"
          @click="startWork"
        ><text>{{ startWorking ? '开始中...' : '开始工作' }}</text></button>
        <button 
          class="end-work-btn" 
          :disabled="!hasOngoingWork || endWorking"
          @click="endWork"
        ><text>{{ endWorking ? '结束中...' : '结束工作' }}</text></button>
      </view>

      <view class="map-container" v-show="!loading && !error">
        <map 
          id="map3D" 
          :longitude="taskData?.longitude" 
          :latitude="taskData?.latitude"
          :markers="mapMarkers"
          class="map"
          :scale="15"
          show-location
          enable-zoom
          enable-scroll
          enable-rotate
        />
        <button @click="moveToTaskLocation" class="move-to-task-btn"><text>定位到任务位置</text></button>
      </view>
      <view class="task-badges">
        <text class="badge-title">完成任务可获得荣誉</text>
        <view v-if="taskData?.badges && taskData?.badges.length > 0">
          <view v-for="badge in taskData?.badges" :key="badge.id" class="badge">
            <text>{{ getBadgeNameByAssociation(badge.association)}}  Lv{{ formatNumber(badge.level) }}</text>
            <text v-if="badge.contributors && taskData?.status === '3'"><text class="badge-label">贡献者:</text></text>
            <view v-if="badge.contributors && taskData?.status === '3'">
              <view v-for="contributor in badge.contributors" :key="contributor.uid">
                <text>UID: {{ formatNumber(contributor.uid) }} - {{ contributor.role }} - {{ formatPercentage(contributor.percentage) }}%</text>
              </view>
            </view>
            <text v-if="badge.contributors && taskData?.status === '3'"><text class="badge-label">评价:</text> {{ badge.evaluation }}</text>
          </view>
        </view>
        <text v-else>暂无徽章</text>
      </view>
      
      <view v-if="(taskData?.status === '1' || taskData?.status === '2') && taskData?.publisherId !== currentUserId" class="challenge-section">
        <view class="form-group">
          <view class="challenge-content-container">
            <textarea 
              v-model="challengeContent" 
              :maxlength="1000"
              :show-confirm-bar="false"
              :cursor-spacing="20"
              placeholder="请详细描述任务的具体要求、功能需求、技术规格等信息..."
              class="challenge-content"
            ></textarea>
            <view class="character-counter">
              <text>{{ formatCharacterCount(challengeContent.length) }}/1000</text>
            </view>
          </view>
        </view>
        <!-- 申请方式选择 -->
        <view class="application-methods">
          <view class="application-mode-selection">
            <view class="mode-option" v-if="canShowTeamMode">
              <checkbox 
                :checked="teamMode" 
                @click="toggleTeamMode"
                class="mode-checkbox"
              />
              <text class="mode-label">组队申请</text>
            </view>
          </view>
          
          <!-- 组队模式表单 -->
          <view v-if="teamMode" class="team-application-form">
            <!-- 团队信息 -->
            <view v-if="teamInfo" class="team-info-section">
              <text class="section-title">团队信息</text>
              <view class="team-info-item">
                <text class="info-label">团队名称：</text>
                <view v-if="isTeamLeader" class="team-name-edit">
                  <input 
                    v-model="editingTeamName" 
                    class="team-name-input" 
                    placeholder="请输入团队名称"
                    maxlength="20"
                    @input="handleTeamNameInput"
                    @focus="handleTeamNameFocus"
                  />
                  <button class="save-name-btn" @click="saveTeamName" :disabled="!editingTeamName.trim()">保存</button>
                </view>
                <view v-else class="team-name-display">
                  <text class="team-name-text">{{ teamInfo.teamName || '临时团队' }}</text>
                  <text class="role-hint">（您不是队长，无法修改团队名称）</text>
                </view>
              </view>
              <view class="team-info-item">
                <text class="info-label">团队状态：</text>
                <text class="info-value">{{ getTeamStatusText(teamInfo.status) }}</text>
              </view>
            </view>
            
            <!-- 团队成员显示 -->
            <view v-if="teamInfo && teamInfo.members && teamInfo.members.length > 0" class="team-members-section">
              <text class="section-title">团队成员 ({{ teamInfo.members.length }})</text>
              <view class="members-list">
                <view 
                  v-for="member in teamInfo.members" 
                  :key="member.userId"
                  class="member-item"
                >
                  <image :src="getUserAvatar(member.userId)" class="member-avatar" @error="handleAvatarError" />
                  <view class="member-info">
                    <text class="member-name">{{ getUserNickname(member.userId) }}</text>
                    <text class="member-role">{{ member.role === 'leader' ? '队长' : '队员' }}</text>
                    <text class="member-status" :class="'status-' + member.status">{{ member.statusText }}</text>
                  </view>
                  <view class="member-actions">
                    <!-- 队长可以踢出队员 -->
                    <button 
                      v-if="isTeamLeader && member.role !== 'leader'"
                      class="remove-member-btn"
                      @click="removeTeamMember(member.userId)"
                    >
                      移除
                    </button>
                    <!-- 队员可以发起踢人投票 -->
                    <button 
                      v-if="!isTeamLeader && member.userId !== currentUserId && member.role !== 'leader'"
                      class="vote-remove-btn"
                      @click="initiateRemoveMemberVote(member.userId)"
                    >
                      发起踢人投票
                    </button>
                    <!-- 队员可以发起弹劾队长投票 -->
                    <button 
                      v-if="!isTeamLeader && member.role === 'leader'"
                      class="impeach-leader-btn"
                      @click="initiateImpeachLeaderVote()"
                    >
                      弹劾队长
                    </button>
                    <!-- 队员可以退出团队 -->
                    <button 
                      v-if="!isTeamLeader && member.userId == currentUserId"
                      class="leave-team-btn"
                      @click="leaveTeam"
                    >
                      退出
                    </button>
                  </view>
                </view>
              </view>
            </view>
            
            <!-- 队长解散团队按钮 -->
            <view v-if="isTeamLeader && teamInfo && teamInfo.members && teamInfo.members.length > 0" class="dissolve-team-section">
              <button 
                class="dissolve-team-btn"
                @click="dissolveTeam"
              >
                解散团队
              </button>
            </view>
            
            <!-- 待处理邀请显示 -->
            <view v-if="teamInfo && teamInfo.pendingInvitations && teamInfo.pendingInvitations.length > 0" class="pending-invitations-section">
              <text class="section-title">待处理邀请 ({{ teamInfo.pendingInvitations.length }})</text>
              <view class="invitations-list">
                <view 
                  v-for="invitation in teamInfo.pendingInvitations" 
                  :key="invitation.userId"
                  class="invitation-item"
                >
                  <image :src="getUserAvatar(invitation.userId)" class="invitation-avatar" @error="handleAvatarError" />
                  <view class="invitation-info">
                    <text class="invitation-name">{{ getUserNickname(invitation.userId) }}</text>
                    <text class="invitation-time">{{ formatTime(invitation.sendTime) }}</text>
                  </view>
                </view>
              </view>
            </view>
            
            <!-- 团队名称输入（仅在没有团队信息时显示） -->
            <view v-if="!teamInfo" class="form-group">
              <text class="form-label">团队名称</text>
              <input 
                v-model="teamName" 
                class="form-input" 
                placeholder="请输入团队名称"
                maxlength="20"
              />
            </view>
            
            <!-- 好友选择（仅队长可见） -->
            <view v-if="isTeamLeader" class="form-group">
              <text class="form-label">邀请成员</text>
              <text class="form-hint">可以邀请好友或搜索其他用户加入团队</text>
              
              
              <view class="friend-search-section">
                <!-- 搜索框 -->
                <view class="search-input-group">
                  <input 
                    v-model="searchKeyword" 
                    class="search-input" 
                    placeholder="输入好友昵称或UID搜索"
                    @input="searchFriends"
                  />
                  <button class="search-btn" @click="searchFriends">搜索</button>
                </view>
                
                <!-- 搜索结果 -->
                <view v-if="searchLoading" class="search-loading">搜索中...</view>
                <view v-else-if="searchError" class="search-error">{{ searchError }}</view>
                <view v-else-if="searchResults.length === 0 && searchKeyword" class="search-empty">
                  没有找到相关好友
                </view>
                <view v-else-if="searchResults.length > 0" class="search-results">
                  <view 
                    v-for="friend in searchResults" 
                    :key="friend.friendId || friend.uid || friend.id"
                    class="search-result-item"
                    :class="{ 
                      'is-friend': friend.friendStatus === 'accepted',
                      'not-friend': friend.friendStatus !== 'accepted'
                    }"
                  >
                    <image :src="getAvatarUrl(friend.avatarUrl || friend.friendAvatarUrl)" class="search-friend-avatar" @error="handleAvatarError" />
                    <view class="search-friend-info">
                      <view class="friend-name-row">
                        <text class="search-friend-name">{{ friend.friendNickname || friend.nickname || '未知用户' }}</text>
                        <view class="friend-status-badge" :class="friend.friendStatus">
                          <text v-if="friend.friendStatus === 'accepted'" class="status-text">好友</text>
                          <text v-else-if="friend.friendStatus === 'pending'" class="status-text">待确认</text>
                          <text v-else class="status-text">非好友</text>
                        </view>
                      </view>
                      <text class="search-friend-uid">UID: {{ friend.friendId || friend.uid || friend.id }}</text>
                    </view>
                    <view class="search-result-actions">
                      <button 
                        v-if="!isUserInvited(friend.friendId || friend.uid || friend.id)"
                        class="invite-btn"
                        @click="inviteSingleUser(friend.friendId || friend.uid || friend.id)"
                      >
                        邀请
                      </button>
                      <view v-else class="invited-status">
                        <text class="invited-text">已邀请</text>
                      </view>
                    </view>
                  </view>
                </view>
                <view v-else class="search-placeholder">
                  请输入好友昵称或UID开始搜索
                </view>
              </view>
            </view>
          </view>
          
          <!-- 申请按钮 -->
          <button 
            v-if="!teamMode || isTeamLeader"
            @click="handleApplication" 
            class="challenge-btn apply-btn"
            :disabled="!challengeContent.trim()"
          >
            <text>{{ teamMode ? '组队申请' : '个人申请' }}</text>
          </button>
          
          <!-- 队员提示 -->
          <view v-if="teamMode && !isTeamLeader" class="member-notice">
            <text class="notice-text">您不是队长，无需提交申请</text>
            <text class="notice-hint">请等待队长提交团队申请</text>
          </view>
        </view>
      </view>
      <!-- 取消申请弹窗 -->
      <view v-if="showCancelRequestModal" class="modal-overlay" @click.self="closeCancelRequestModal">
        <view class="modal-content" @click.stop>
          <view class="modal-header">
            <text class="modal-title">申请取消任务</text>
            <button class="modal-close" @click="closeCancelRequestModal"><text>×</text></button>
          </view>
          <view class="modal-body">
            <view class="form-group">
              <text>取消原因：</text>
              <textarea 
                v-model="cancelReason" 
                :maxlength="200"
                :show-confirm-bar="false"
                :cursor-spacing="20"
                placeholder="请说明取消任务的原因..."
                class="cancel-reason-textarea"
              ></textarea>
              <view class="character-counter">
                <text>{{ formatCharacterCount(cancelReason.length) }}/200</text>
              </view>
            </view>
          </view>
          <view class="modal-footer">
            <button class="modal-cancel-btn" @click="closeCancelRequestModal">
              <text>取消</text>
            </button>
            <button class="modal-confirm-btn" @click="submitCancelRequest" :disabled="!cancelReason.trim()">
              <text>提交申请</text>
            </button>
          </view>
        </view>
      </view>

      <view id="apply-complete-anchor"></view>
      
      <!-- 申请完成详情显示（发布者查看对方提交的申请） -->
      <view v-if="isPublisherAndInProgress && showCompletePreview" class="apply-complete-preview-section">
        <text class="apply-complete-title">申请完成详情</text>
        <view class="form-group">
          <text>完成证明：</text>
          <view class="complete-proof-container">
            <view class="preview-proof">{{ completePreview?.proof || '—' }}</view>
          </view>
        </view>
        <view class="form-group">
          <text>铭牌分配：</text>
          <view v-if="completePreview?.badges && completePreview.badges.length">
            <view v-for="b in completePreview.badges" :key="b.badgeId" class="badge-form">
              <view>
                <text>{{ getBadgeNameByAssociation(b.association || b.assoc) || '徽章' }} Lv{{ formatNumber(b.level || 0) }}</text>
              </view>
              <view v-for="(c, idx) in b.contributors" :key="idx" class="contributor-form">
                <text>贡献者</text>
                <view class="contributor-selector">
                  <image 
                    :src="getSafeUserAvatar(c.uid)" 
                    class="contributor-avatar-small" 
                    mode="aspectFill"
                    @error="handleAvatarError"
                  />
                  <text class="contributor-name">{{ getUserName(c.uid) }}</text>
                </view>
                <text>角色</text>
                <view class="preview-field">{{ c.role || '—' }}</view>
                <text>贡献比例(%)</text>
                <view class="preview-field">{{ formatPercentage(c.percentage) }}%</view>
              </view>
            </view>
          </view>
          <text v-else>暂无数据</text>
        </view>
      </view>
      
      <view v-if="showApplyCompleteBtn" class="apply-complete-section">
        <text class="apply-complete-title">申请完成任务</text>
        <view class="form-group">
          <text>完成证明：</text>
          <view class="complete-proof-container">
            <textarea 
              v-model="completeProof" 
              :maxlength="500"
              :show-confirm-bar="false"
              :cursor-spacing="20"
              placeholder="请填写完成证明"
              class="complete-proof-textarea"
            ></textarea>
            <view class="character-counter">
              <text>{{ formatCharacterCount(completeProof.length) }}/500</text>
            </view>
          </view>
        </view>
        <view class="form-group" v-if="canShowBadgeAllocation">
          <text>铭牌分配：</text>
          <view v-for="badge in taskData?.badges" :key="badge.id" class="badge-form">
            <view>
              <text>{{ getBadgeNameByAssociation(badge.association) }} Lv{{ formatNumber(badge.level) }}</text>
            </view>
            <view v-for="(contributor, index) in badgeContributors[badge.id]" :key="index" class="contributor-form">
              <text>分配给队员</text>
              <view class="contributor-selector">
                <TeamMemberSelector
                  v-if="!(index === 0 && contributor.uid === currentUserId)"
                  :currentUserId="currentUserId"
                  :disabled="false"
                  :teamMembers="getTeamMembersForBadge()"
                  @select="(member) => selectContributor(badge.id, index, member)"
                  @clear="() => clearContributor(badge.id, index)"
                />
                <view v-else class="current-user-display">
                  <text>当前用户 ({{ currentUserId }})</text>
                </view>
              </view>
              <text>角色</text>
              <input 
                type="text" 
                v-model="contributor.role" 
                placeholder="可选，如：开发者/设计师"
              />
              <text>贡献比例(%)</text>
              <input 
                type="number" 
                v-model.number="contributor.percentage" 
                min="1" 
                max="100" 
                @input="debouncedValidateTotalPercentage(badge.id)"
              />
              <button 
                v-if="!(index === 0 && contributor.uid === currentUserId)" 
                @click="removeContributor(badge.id, index)" 
                class="remove-btn"
              >
                <text>删除</text>
              </button>
            </view>
            <view class="percentage-total">
              <text>总百分比: {{ formatPercentage(calculateTotalPercentage(badge.id)) }}% </text>
              <text v-if="calculateTotalPercentage(badge.id) !== 100" class="error-text">(必须等于100%)</text>
            </view>
            <button @click="addContributor(badge.id)" class="add-btn"><text>添加贡献者</text></button>
          </view>
        </view>
        <button 
          v-if="!hasAppliedCompletion"
          @click="applyCompleteTask" 
          class="apply-complete-btn"
          :disabled="!canSubmitComplete"
        ><text>申请完成任务</text></button>
        <view v-else class="applied-actions">
          <button class="apply-complete-btn" disabled><text>已申请完成</text></button>
          <button class="cancel-apply-btn" @click="cancelCompletionByTask"><text>撤回完成申请</text></button>
        </view>
      </view>
      
      <!-- 申请完成处理按钮（页面底部） -->
      <view v-if="isPublisherAndInProgress && showCompletePreview" class="completion-actions-bottom">
        <button class="reject-completion-btn" @click="handleRejectCompletion">
          <text>驳回申请</text>
        </button>
        <button class="approve-completion-btn" @click="handleApproveCompletion">
          <text>同意申请</text>
        </button>
    </view>
      
    </view>
    
    <!-- 组队邀请模态框 -->
    <TeamInvitationModal 
      :show="showTeamInvitationModal"
      :taskId="taskData?.taskId"
      :teamInfo="teamInfo"
      @close="closeTeamInvitationModal"
      @confirm="handleTeamInvitationConfirm"
      @updateTeamName="handleUpdateTeamName"
    />
  </scroll-view>
</template>

<script>
import { getBadgeNameByAssociation } from "@/utils/skillUtils";
import { ref, computed, watch, reactive } from "vue";
import { onLoad, onShow, onHide, onUnload } from '@dcloudio/uni-app';
import { useUserStore } from '@/store/user'
import { getAvatarUrl } from '@/utils/imageUtils.js'
import FriendSelector from '@/components/FriendSelector.vue'
import TeamInvitationModal from '@/components/TeamInvitationModal.vue'

export default {
  name: "TaskDetail",
  components: {
    FriendSelector,
    TeamInvitationModal
  },
  setup() {
    let map3D = null;
    let marker3D = null;
    const task = ref(null);
    const loading = ref(true);
    const error = ref(null);
    let taskId = null;
    const refreshing = ref(false);
    const scrollIntoView = ref('');
    const mapMarkers = computed(() => {
      if (!task.value || !task.value.longitude || !task.value.latitude) {
        return [{
          id: 1,
          longitude: 116.397128,
          latitude: 39.916527,
          title: '默认位置',
          width: 30,
          height: 30
        }];
      }
      return [{
        id: 1,
        longitude: parseFloat(task.value.longitude),
        latitude: parseFloat(task.value.latitude),
        title: task.value.title || '任务位置',
        width: 30,
        height: 30
      }];
    });
    const applyForTask = async () => {
      if (!challengeContent.value.trim()) {
        uni.showToast({ title: "请输入申请理由！", icon: 'none' });
        return;
      }
      uni.showLoading({ title: '提交申请中...' });
      try {
        const token = uni.getStorageSync("accessToken");
        if (!token) {
          uni.showToast({ title: "请先登录", icon: 'none' });
          return;
        }
        const url = `${NETWORK_CONFIG.API_BASE_URL}/task/apply?taskId=${task.value.taskId}&content=${encodeURIComponent(challengeContent.value)}`;
        const response = await uni.request({
          url: url,
          method: 'POST',
          header: {
            'Content-Type': 'application/json',
            'Access-Token': `Bearer ${token}`,
          },
        });
        if (response.statusCode === 200) {
          uni.hideLoading();
          uni.showToast({ title: response.data.message || "申请成功！", icon: 'success' });
          // 申请成功后刷新页面
          await fetchTaskDetail(task.value.taskId);
          await checkUserApplied(); // 重新检查用户申请状态
        } else {
          throw new Error(response.data?.message || `申请失败，状态码: ${response.statusCode}`);
        }
      } catch (err) {
        uni.hideLoading();
        uni.showToast({ title: err.message || "申请失败，请稍后重试！", icon: 'none', duration: 3000 });
      }
    };
    const getStatusText = (status) => {
      switch (status) {
        case "0": return "审核中";
        case "1": return "未完成";
        case "2": return "进行中";
        case "3": return "已完成";
        default: return "未知状态";
      }
    };
    const addContributor = (badgeId) => {
      if (!badgeContributors[badgeId]) badgeContributors[badgeId] = [];
      badgeContributors[badgeId].push({ uid: '', role: '', percentage: 0 });
      redistributePercentage(badgeId);
    };
    const removeContributor = (badgeId, index) => {
      badgeContributors[badgeId].splice(index, 1);
      redistributePercentage(badgeId);
    };
    const selectContributor = (badgeId, index, member) => {
      if (badgeContributors[badgeId] && badgeContributors[badgeId][index]) {
        badgeContributors[badgeId][index].uid = member.userId;
        badgeContributors[badgeId][index].nickname = member.nickname;
      }
    };
    const clearContributor = (badgeId, index) => {
      if (badgeContributors[badgeId] && badgeContributors[badgeId][index]) {
        badgeContributors[badgeId][index].uid = '';
        badgeContributors[badgeId][index].nickname = '';
      }
    };
    
    // 获取团队成员数据，转换为TeamMemberSelector需要的格式
    const getTeamMembersForBadge = () => {
      if (!teamInfo.value || !teamInfo.value.members) {
        return [];
      }
      
      return teamInfo.value.members.map(member => ({
        userId: member.userId,
        nickname: getUserNickname(member.userId),
        avatarUrl: getUserAvatar(member.userId),
        role: member.role
      }));
    };
    const calculateTotalPercentage = (badgeId) => {
      if (!badgeContributors[badgeId]) return 0;
      return badgeContributors[badgeId].reduce((sum, contributor) => sum + (contributor.percentage || 0), 0);
    };
    const validateTotalPercentage = (badgeId) => {
      const total = calculateTotalPercentage(badgeId);
    };
    const redistributePercentage = (badgeId) => {
      const contributors = badgeContributors[badgeId];
      if (!contributors || contributors.length === 0) return;
      if (contributors.length === 1) {
        contributors[0].percentage = 100;
        return;
      }
      const equalShare = Math.floor(100 / contributors.length);
      const remainder = 100 - (equalShare * contributors.length);
      contributors.forEach((contributor, index) => {
        contributor.percentage = equalShare + (index === 0 ? remainder : 0);
      });
    };
    const defaultAvatar = "https://example.com/default-avatar.jpg";
    const userStore = useUserStore();
    const challengeContent = ref("");
    const completeProof = ref('');
    const showCompletePreview = ref(false);
    const previewLoading = ref(false);
    const previewError = ref('');
    const completePreview = ref(null);
    
    
    // 任务取消相关状态
    const showCancelRequestModal = ref(false);
    const cancelReason = ref('');
    const taskStatus = ref(null);
    const badgeContributions = reactive({});
    const badgeContributors = reactive({});
    const initializeBadgeContributors = () => {
      if (task.value && Array.isArray(task.value.badges)) {
        task.value.badges.forEach(badge => {
          badgeContributors[badge.id] = [{
            uid: currentUserId.value,
            role: '主要贡献者',
            percentage: 100
          }];
        });
      }
    };
    watch(() => task.value, (newTask) => {
      if (newTask && Array.isArray(newTask.badges)) {
        newTask.badges.forEach(badge => { badgeContributions[badge.id] = 100; });
        initializeBadgeContributors();
      }
    }, { immediate: true });
    const showApplyCompleteBtn = computed(() => {
      if (!taskData.value || !currentUserId.value) {
        console.log('showApplyCompleteBtn: 缺少必要数据', { 
          hasTaskData: !!taskData.value, 
          hasCurrentUserId: !!currentUserId.value 
        });
        return false;
      }
      const challengerId = taskData.value.challengerId;
      const status = taskData.value.status;
      const result = challengerId && String(challengerId) === String(currentUserId.value) && String(status) === '2';
      console.log('showApplyCompleteBtn 计算结果:', {
        challengerId,
        currentUserId: currentUserId.value,
        status,
        challengerMatch: challengerId && String(challengerId) === String(currentUserId.value),
        statusMatch: String(status) === '2',
        result
      });
      return result;
    });
    const isPublisherAndInProgress = computed(() => {
      if (!taskData.value || !currentUserId.value) return false;
      return String(taskData.value.publisherId) === String(currentUserId.value) && String(taskData.value.status) === '2';
    });

    // 任务操作相关计算属性
    const showTaskActions = computed(() => {
      if (!taskData.value || !currentUserId.value) return false;
      const status = taskData.value.status;
      const publisherId = taskData.value.publisherId;
      const challengerId = taskData.value.challengerId;
      const isPublisher = String(publisherId) === String(currentUserId.value);
      const isChallenger = challengerId && String(challengerId) === String(currentUserId.value);
      
      console.log('showTaskActions 调试信息:', {
        isPublisher,
        isChallenger,
        hasUserApplied: hasUserApplied.value,
        status: taskData.value?.status,
        publisherId: taskData.value?.publisherId,
        challengerId: taskData.value?.challengerId,
        currentUserId: currentUserId.value,
        result: isPublisher || isChallenger || hasUserApplied.value
      });
      
      // 发布者、挑战者或申请者都可以看到操作按钮
      return isPublisher || isChallenger || hasUserApplied.value;
    });

    const canCancelApply = computed(() => {
      if (!taskData.value || !currentUserId.value) return false;
      const status = taskData.value.status;
      const challengerId = taskData.value.challengerId;
      const isChallenger = challengerId && String(challengerId) === String(currentUserId.value);
      
      console.log('canCancelApply 调试信息:', {
        status,
        challengerId,
        isChallenger,
        hasUserApplied: hasUserApplied.value,
        currentUserId: currentUserId.value,
        result: (isChallenger && String(status) === '1') || (String(status) === '1' && hasUserApplied.value)
      });
      
      // 只有申请者可以撤回申请，且任务状态为1（可申请状态）
      // 或者当前用户申请了任务但challengerId还未设置（通过消息记录判断）
      return (isChallenger && String(status) === '1') || 
             (String(status) === '1' && hasUserApplied.value);
    });

    const canRequestCancel = computed(() => {
      if (!taskData.value || !currentUserId.value) return false;
      const status = taskData.value.status;
      const publisherId = taskData.value.publisherId;
      const challengerId = taskData.value.challengerId;
      const isPublisher = String(publisherId) === String(currentUserId.value);
      const isChallenger = challengerId && String(challengerId) === String(currentUserId.value);
      
      // 只有任务相关方可以申请取消，且任务状态为2（进行中）
      return (isPublisher || isChallenger) && String(status) === '2';
    });

    const canDeleteTask = computed(() => {
      if (!taskData.value || !currentUserId.value) return false;
      const status = taskData.value.status;
      const publisherId = taskData.value.publisherId;
      const isPublisher = String(publisherId) === String(currentUserId.value);
      
      // 只有发布者可以删除任务，且任务状态为0或1
      return isPublisher && (String(status) === '0' || String(status) === '1');
    });

    // 检查是否有待处理的消息
    const hasPendingMessages = ref(false);
    const pendingMessageTypes = ref([]);
    const hasAppliedCompletion = ref(false);
    
    // 检查用户是否申请了该任务
    const hasUserApplied = ref(false);
    
    // 组队相关状态
    const showTeamInvitationModal = ref(false);
    const teamInfo = ref(null);
    const teamMembers = ref([]);
    const teamStatus = ref(null); // 0-组建中, 1-已组建, 2-已解散
    
    // 新的组队模式状态
    const teamMode = ref(false); // 是否启用组队模式
    const teamName = ref(''); // 团队名称
    const editingTeamName = ref(''); // 编辑中的团队名称
    const userInfoCache = ref({}); // 用户信息缓存
    
    // 搜索相关状态
    const searchKeyword = ref('');
    const searchResults = ref([]);
    const searchLoading = ref(false);
    const searchError = ref(null);
    const invitedUsers = ref(new Set()); // 已邀请的用户
    const isTeamLeader = ref(false); // 是否是队长

    // 解析JWT token的工具函数（兼容微信小程序）
    const parseJwtToken = (token) => {
      try {
        const tokenParts = token.split('.');
        if (tokenParts.length !== 3) {
          throw new Error('Invalid token format');
        }
        
        const payloadBase64 = tokenParts[1];
        // 添加填充字符以确保 base64 长度正确
        const paddedBase64 = payloadBase64 + '='.repeat((4 - payloadBase64.length % 4) % 4);
        
        // 在微信小程序环境中使用 uni.base64ToArrayBuffer
        const arrayBuffer = uni.base64ToArrayBuffer(paddedBase64);
        const payloadString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
        return JSON.parse(payloadString);
      } catch (error) {
        console.error('解析JWT token失败:', error);
        return null;
      }
    };

    // 获取用户消息状态
    const checkPendingMessages = async () => {
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) return;
        
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) return;
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/message/pending?taskId=${taskIdVal}`,
          method: 'GET',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        
        if (response.statusCode === 200 && response.data) {
          hasPendingMessages.value = response.data.hasPending;
          pendingMessageTypes.value = response.data.messageTypes || [];
          // 注意：pendingMessageTypes 是别人发给我的待处理消息，不用于判断我是否已申请完成
          // hasAppliedCompletion 应该通过专门的 check-completion-apply 接口来设置
        }
      } catch (e) {
        console.log('检查待处理消息失败:', e);
      }
    };

    // 检查用户是否申请了该任务
    const checkUserApplied = async () => {
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) return;
        
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) return;
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/message/check-apply?taskId=${taskIdVal}`,
          method: 'GET',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        
        if (response.statusCode === 200 && response.data) {
          hasUserApplied.value = response.data.hasApplied || false;
          console.log('检查用户申请状态结果:', {
            taskId: taskIdVal,
            hasApplied: hasUserApplied.value,
            response: response.data
          });
          
          // 强制触发响应式更新
          console.log('hasUserApplied 更新后:', hasUserApplied.value);
          
          // 强制触发计算属性重新计算
          setTimeout(() => {
            console.log('延迟检查 canCancelApply:', {
              hasUserApplied: hasUserApplied.value,
              status: taskData.value?.status,
              hasPendingMessages: hasPendingMessages.value,
              canCancelApply: canCancelApply.value,
              showTaskActions: showTaskActions.value
            });
          }, 100);
        }
        // 同时检查是否有未处理的完成申请，驱动“撤回完成申请”按钮显示
        try {
          const resp2 = await uni.request({
            url: `${NETWORK_CONFIG.API_BASE_URL}/message/check-completion-apply?taskId=${taskIdVal}`,
            method: 'GET',
            header: { 'Access-Token': `Bearer ${token}` },
            timeout: 8000
          });
          if (resp2.statusCode === 200 && resp2.data) {
            hasAppliedCompletion.value = !!resp2.data.hasApplied;
          }
        } catch (_) {}
      } catch (e) {
        console.log('检查用户申请状态失败:', e);
      }
    };
    const taskData = computed(() => {
      if (!task.value) return null;
      
      return {
        title: task.value.title || '',
        userName: task.value.publisherName || task.value.userName || '未知用户',
        orgName: task.value.orgName || '未知企业',
        publishTime: task.value.publishTime || '',
        deadline: task.value.deadline || '',
        description: task.value.description || '',
        reward: task.value.reward || 0,
        currentPaid: task.value.currentPaid || 0,
        status: task.value.status || '',
        challengerId: task.value.challengerId || null,
        publisherId: task.value.publisherId || null,
        userAvatar: getAvatarUrl(task.value.userAvatar),
        longitude: task.value.longitude || 116.397128,
        latitude: task.value.latitude || 39.916527,
        badges: task.value.badges || [],
        taskId: task.value.taskId || null
      };
    });
    const currentUserId = computed(() => userStore.profile?.uid || null);
    
    // 是否可以显示组队模式复选框
    const canShowTeamMode = computed(() => {
      // 如果任务状态为2（进行中），禁止创建团队
      if (taskData.value?.status === '2') {
        return false;
      }
      // 如果用户是挑战者且没有团队，不显示组队模式
      if (taskData.value?.challengerId === currentUserId.value && !teamInfo.value) {
        return false;
      }
      return true;
    });
    
    // 是否可以显示铭牌分配
    const canShowBadgeAllocation = computed(() => {
      // 如果任务状态为2（进行中），不显示铭牌分配
      if (taskData.value?.status === '2') {
        return false;
      }
      // 如果用户是挑战者且没有团队，不显示铭牌分配
      if (taskData.value?.challengerId === currentUserId.value && !teamInfo.value) {
        return false;
      }
      return true;
    });
    const moveToTaskLocation = () => {
      if (!task.value || !task.value.longitude || !task.value.latitude) {
        uni.showToast({ title: '任务位置信息无效', icon: 'none' });
        return;
      }
      const mpLongitude = parseFloat(task.value.longitude);
      const mpLatitude = parseFloat(task.value.latitude);
      if (isNaN(mpLongitude) || isNaN(mpLatitude)) {
        uni.showToast({ title: '任务坐标无效', icon: 'none' });
        return;
      }
      // 关键代码：让地图回正
      const mapCtx = uni.createMapContext('map3D');
      mapCtx.moveToLocation({
        longitude: mpLongitude,
        latitude: mpLatitude
      });
    };
    const fetchTaskDetail = async (taskId) => {
      try {
        const networkType = await uni.getNetworkType();
        if (networkType.networkType === 'none') {
          throw new Error('网络连接不可用，请检查网络设置');
        }
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/task/detail`,
          method: 'GET',
          data: { taskId },
          timeout: 10000
        });
        if (response.statusCode === 200) {
          let data = response.data;
          // 后端可能返回字符串形式的JSON，做一次安全解析
          if (typeof data === 'string') {
            try { data = JSON.parse(data); } catch (_) {}
          }
          task.value = data;
          // 若详情内带有工作记录（TaskService.detail 已包含 workTimes），初始化它
          try {
            const rawWorkTimes = data?.workTimes;
            if (rawWorkTimes) {
              if (Array.isArray(rawWorkTimes)) {
                workTimes.value = rawWorkTimes;
              } else if (typeof rawWorkTimes === 'string') {
                const parsed = JSON.parse(rawWorkTimes);
                if (Array.isArray(parsed)) workTimes.value = parsed;
              }
            }
          } catch (_) {}
          console.log('任务详情加载成功:', {
            taskData: response.data,
            challengerId: response.data?.challengerId,
            status: response.data?.status,
            publisherId: response.data?.publisherId,
            publisherName: response.data?.publisherName,
            orgName: response.data?.orgName,
            userAvatar: response.data?.userAvatar
          });
        } else {
          throw new Error(`服务器响应错误: ${response.statusCode}`);
        }
      } catch (err) {
        error.value = `加载任务详情失败: ${err.message}`;
      } finally {
        loading.value = false;
      }
    };
    const onRefresh = async () => {
      if (!taskId) return;
      refreshing.value = true;
      try {
        await fetchTaskDetail(taskId);
        uni.showToast({ title: '刷新成功', icon: 'success' });
      } catch (err) {
        uni.showToast({ title: '刷新失败', icon: 'none' });
      } finally {
        refreshing.value = false;
      }
    };
    const fetchApplyCompletePreview = async () => {
      previewError.value = '';
      previewLoading.value = true;
      completePreview.value = null;
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) throw new Error('请先登录');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) throw new Error('任务 ID 缺失');
        // 使用新的详情接口
        const tryUrls = [
          `${NETWORK_CONFIG.API_BASE_URL}/message/applycomplete/detail?taskId=${taskIdVal}`
        ];
        let data = null;
        for (let i = 0; i < tryUrls.length; i++) {
          const res = await uni.request({
            url: tryUrls[i],
            method: 'GET',
            header: { 'Access-Token': `Bearer ${token}` },
            timeout: 8000
          });
          if (res.statusCode === 200 && res.data) {
            data = res.data;
            break;
          }
        }
        if (!data) throw new Error('未获取到申请完成详情');
        
        // 获取所有贡献者的UID
        const contributorUids = [];
        if (Array.isArray(data.badges)) {
          data.badges.forEach(badge => {
            if (Array.isArray(badge.contributors)) {
              badge.contributors.forEach(contributor => {
                if (contributor.uid) {
                  contributorUids.push(contributor.uid);
                }
              });
            }
          });
        }
        
        // 批量获取用户信息
        if (contributorUids.length > 0) {
          await fetchUsersInfo(contributorUids);
        }
        
        // 兼容字段：proof、badges[{ badgeId, contributors[], association/assoc, level }]、messageId
        completePreview.value = {
          proof: data.proof || data.reason || data.description || '',
          badges: Array.isArray(data.badges) ? data.badges : [],
          messageId: data.messageId,
          taskId: data.taskId,
          createTime: data.createTime,
          status: data.status
        };
      } catch (e) {
        previewError.value = e.message || '加载失败';
      } finally {
        previewLoading.value = false;
      }
    };
    const viewApplyCompleteInfo = async () => {
      showCompletePreview.value = true;
      await fetchApplyCompletePreview();
    };
    const closeCompletePreview = () => {
      showCompletePreview.value = false;
    };
    
    // 获取用户信息
    const fetchUserInfo = async (uid) => {
      if (userInfoCache.value[uid]) {
        return userInfoCache.value[uid];
      }
      
      try {
        const token = uni.getStorageSync('accessToken');
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/user/info`,
          method: 'POST',
          header: { 
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          },
          data: { uid: uid }
        });
        
        if (response.statusCode === 200 && response.data) {
          userInfoCache.value[uid] = response.data;
          return response.data;
        }
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
      
      return null;
    };
    
    // 批量获取用户信息
    const fetchUsersInfo = async (uids) => {
      const promises = uids.map(uid => fetchUserInfo(uid));
      await Promise.all(promises);
    };
    
    
    // 获取安全的用户头像（带错误处理）
    const getSafeUserAvatar = (uid) => {
      const userInfo = userInfoCache.value[uid];
      if (userInfo?.avatarUrl) {
        // 检查头像URL是否有效
        if (userInfo.avatarUrl.startsWith('/uploads/')) {
          return `${NETWORK_CONFIG.API_BASE_URL}${userInfo.avatarUrl}`;
        } else if (userInfo.avatarUrl.startsWith('http')) {
          return userInfo.avatarUrl;
        }
      }
      // 使用默认头像
      return '/static/images/default-avatar.png';
    };
    
    // 获取用户昵称
    const getUserName = (uid) => {
      const userInfo = userInfoCache.value[uid];
      return userInfo?.nickname || `用户${uid}`;
    };
    
    // 头像加载错误处理
    const handleAvatarError = (e) => {
      console.log('头像加载失败:', e);
      // 可以在这里设置默认头像
      e.target.src = '/static/images/default-avatar.png';
    };
    
    // 任务发布者头像错误处理
    const handleTaskAvatarError = () => {
      console.log('任务发布者头像加载失败，使用默认头像');
    };
    
    // 组队相关方法
    
    // 切换组队模式
    const toggleTeamMode = async (e) => {
      console.log('Checkbox click event:', e);
      console.log('切换前 teamMode:', teamMode.value);
      
      if (teamMode.value) {
        // 当前是开启状态，用户要关闭
        teamMode.value = false;
        isTeamLeader.value = false;
        teamInfo.value = null;
        console.log('关闭组队模式，重置队长状态');
      } else {
        // 当前是关闭状态，用户要开启
        teamMode.value = true;
        console.log('启用组队模式，开始检查团队状态...');
        
        // 先检查是否已有团队
        await checkExistingTeam();
        
        // 如果检查后没有团队信息，则创建新团队
        if (!teamInfo.value) {
          console.log('检查后没有团队信息，创建新团队');
          await createTeamImmediately();
        } else {
          console.log('检查后发现有团队信息，用户角色:', isTeamLeader.value ? '队长' : '队员');
        }
        
        // 确保表单显示
        console.log('组队模式已启用，表单应该显示');
      }
    };
    
    // 立即创建团队
    const createTeamImmediately = async () => {
      try {
        const token = uni.getStorageSync('accessToken');
        const currentUser = uni.getStorageSync('userInfo');
        
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' });
          return;
        }
        
        let leaderId = null;
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid;
        } else {
          try {
            const tokenPayload = parseJwtToken(token);
            if (tokenPayload) {
              leaderId = tokenPayload.uid || tokenPayload.sub;
            }
          } catch (tokenError) {
            console.error('解析token失败:', tokenError);
            uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' });
            return;
          }
        }
        
        if (!leaderId) {
          uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' });
          return;
        }
        
        console.log('立即创建团队，参数:', {
          taskId: taskData.value?.taskId,
          leaderId: leaderId,
          teamName: teamName.value.trim() || '临时团队'
        });
        
        // 创建团队
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/create-invitation`,
          method: 'POST',
          data: {
            taskId: Number(taskData.value?.taskId),
            leaderId: Number(leaderId),
            teamName: teamName.value.trim() || '临时团队',
            inviteeIds: [] // 暂时不邀请任何人，只创建团队
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        console.log('团队创建响应:', response);
        
        if (response.data && response.data.success) {
          // 根据是否已存在显示不同消息
          if (response.data.isExisting) {
            uni.showToast({ title: '团队已存在', icon: 'success' });
          } else {
            uni.showToast({ title: '已为您创建团队', icon: 'success' });
          }
          
          // 保存团队信息到响应式数据
          if (response.data.teamInfo) {
            teamInfo.value = response.data.teamInfo;
            editingTeamName.value = teamInfo.value.teamName || '临时团队';
            
            // 设置队长状态 - 创建团队的用户自动成为队长
            isTeamLeader.value = true;
            console.log('团队信息:', teamInfo.value);
            console.log('设置editingTeamName为:', editingTeamName.value);
            console.log('用户角色: 队长');
            
            // 自动获取团队成员的用户信息
            if (teamInfo.value.members && teamInfo.value.members.length > 0) {
              const memberIds = teamInfo.value.members.map(member => member.userId);
              console.log('获取团队成员信息，memberIds:', memberIds);
              await fetchUsersInfo(memberIds);
            }
          }
        } else {
          uni.showToast({ title: response.data?.message || '团队创建失败', icon: 'none' });
        }
      } catch (error) {
        console.error('创建团队失败:', error);
        uni.showToast({ title: '创建团队失败', icon: 'none' });
      }
    };
    
    // 检查现有团队
    const checkExistingTeam = async () => {
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) return;
        
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) return;
        
        console.log('检查现有团队，任务ID:', taskIdVal);
        
        // 获取当前用户ID
        const currentUser = uni.getStorageSync('userInfo');
        let leaderId = null;
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid;
        } else {
          try {
            const tokenPayload = parseJwtToken(token);
            if (tokenPayload) {
              leaderId = tokenPayload.uid || tokenPayload.sub;
            }
          } catch (tokenError) {
            console.error('解析token失败:', tokenError);
            return;
          }
        }
        
        if (!leaderId) return;
        
        // 调用后端API检查是否已有团队
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/info?taskId=${taskIdVal}&userId=${leaderId}`,
          method: 'GET',
          header: {
            'Access-Token': `Bearer ${token}`
          }
        });
        
        console.log('检查团队信息响应:', response);
        
        if (response.statusCode === 200 && response.data && response.data.success) {
          if (response.data.teamInfo) {
            // 有团队信息，设置组队模式为true
            teamMode.value = true;
            teamInfo.value = response.data.teamInfo;
            editingTeamName.value = teamInfo.value.teamName || '临时团队';
            
            // 判断用户是否是队长
            console.log('团队信息:', teamInfo.value);
            console.log('teamInfo.leaderId:', teamInfo.value.leaderId, '类型:', typeof teamInfo.value.leaderId);
            console.log('当前用户ID:', leaderId, '类型:', typeof leaderId);
            console.log('严格相等比较:', teamInfo.value.leaderId === leaderId);
            console.log('宽松相等比较:', teamInfo.value.leaderId == leaderId);
            
            isTeamLeader.value = teamInfo.value.leaderId == leaderId;
            console.log('发现现有团队，启用组队模式:', teamInfo.value);
            console.log('用户角色:', isTeamLeader.value ? '队长' : '队员');
            
            // 获取团队成员的用户信息
            if (teamInfo.value.members && teamInfo.value.members.length > 0) {
              const memberIds = teamInfo.value.members.map(member => member.userId);
              console.log('获取现有团队成员信息，memberIds:', memberIds);
              await fetchUsersInfo(memberIds);
              
              // 初始化已邀请用户列表 - 将现有团队成员标记为已邀请
              memberIds.forEach(memberId => {
                invitedUsers.value.add(memberId);
              });
              console.log('初始化已邀请用户列表:', Array.from(invitedUsers.value));
            }
          } else {
            // 没有团队信息，保持组队模式为false
            teamMode.value = false;
            isTeamLeader.value = false;
            console.log('没有现有团队，保持个人模式');
          }
        } else {
          // API调用失败，保持默认状态
          teamMode.value = false;
          isTeamLeader.value = false;
          console.log('检查团队信息失败，保持个人模式');
        }
      } catch (error) {
        console.error('检查现有团队失败:', error);
        // 出错时保持默认状态
        teamMode.value = false;
      }
    };
    
    // 统一的申请处理方法
    const handleApplication = async () => {
      if (teamMode.value) {
        await handleTeamApplication();
      } else {
        await applyForTask();
      }
    };
    
    // 组队申请处理
    const handleTeamApplication = async () => {
      try {
        // 团队已经在勾选时创建了，现在只需要进行个人申请
        if (teamInfo.value) {
          await applyForTask();
        } else {
          uni.showToast({ title: '团队信息不存在，请重新勾选组队申请', icon: 'none' });
        }
      } catch (error) {
        console.error('组队申请失败:', error);
        uni.showToast({ title: '组队申请失败', icon: 'none' });
      }
    };
    
    // 从TeamInvitationModal复制的方法
    const getTeamStatusText = (status) => {
      switch (status) {
        case 0: return '组建中'
        case 1: return '已组建'
        case 2: return '已解散'
        default: return '未知状态'
      }
    };
    
    const formatTime = (timeStr) => {
      if (!timeStr) return ''
      const date = new Date(timeStr)
      const now = new Date()
      const diff = now - date
      
      if (diff < 60000) return '刚刚'
      if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
      if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
      return Math.floor(diff / 86400000) + '天前'
    };
    
    // 获取用户信息
    const getUserInfo = async (userId) => {
      if (userInfoCache.value[userId]) {
        return userInfoCache.value[userId]
      }
      
      try {
        const token = uni.getStorageSync('accessToken')
        if (!token) return null
        
        console.log('获取用户信息，userId:', userId)
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/user/info`,
          method: 'POST',
          data: {
            uid: userId
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        console.log('用户信息响应:', response)
        
        if (response.statusCode === 200 && response.data) {
          const userInfo = response.data
          userInfoCache.value[userId] = userInfo
          console.log('用户信息缓存成功:', userInfo)
          return userInfo
        } else {
          console.error('获取用户信息失败，响应:', response.data)
        }
      } catch (error) {
        console.error('获取用户信息失败:', error)
      }
      
      return null
    };
    
    // 获取用户昵称
    const getUserNickname = (userId) => {
      const userInfo = userInfoCache.value[userId]
      return userInfo ? userInfo.nickname : '加载中...'
    };
    
    // 获取用户头像
    const getUserAvatar = (userId) => {
      const userInfo = userInfoCache.value[userId]
      console.log('获取用户头像，userId:', userId, 'userInfo:', userInfo)
      
      if (userInfo && userInfo.avatarUrl) {
        if (userInfo.avatarUrl.startsWith('/')) {
          const fullUrl = `${NETWORK_CONFIG.API_BASE_URL}${userInfo.avatarUrl}`
          console.log('头像URL（相对路径）:', fullUrl)
          return fullUrl
        }
        console.log('头像URL（绝对路径）:', userInfo.avatarUrl)
        return userInfo.avatarUrl
      }
      
      console.log('使用默认头像')
      return '/static/images/default-avatar.png'
    };
    
    // 搜索好友
    const searchFriends = async () => {
      if (!searchKeyword.value.trim()) {
        searchResults.value = []
        return
      }
      
      searchLoading.value = true
      searchError.value = null
      
      try {
        const token = uni.getStorageSync('accessToken')
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/friends/search?keyword=${encodeURIComponent(searchKeyword.value)}`,
          method: 'GET',
          header: {
            'Access-Token': `Bearer ${token}`
          }
        })
        
        if (response.data && response.data.code === 200) {
          const allResults = response.data.data || []
          searchResults.value = allResults.sort((a, b) => {
            if (a.friendStatus === 'accepted' && b.friendStatus !== 'accepted') return -1
            if (a.friendStatus !== 'accepted' && b.friendStatus === 'accepted') return 1
            return 0
          })
        } else {
          searchError.value = response.data?.message || '搜索失败'
        }
      } catch (error) {
        console.error('搜索好友失败:', error)
        searchError.value = '搜索失败，请重试'
      } finally {
        searchLoading.value = false
      }
    };
    
    
    
    
    // 邀请单个用户
    const inviteSingleUser = async (friendId) => {
      try {
        const token = uni.getStorageSync('accessToken')
        const currentUser = uni.getStorageSync('userInfo')
        
        let leaderId = null
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid
        } else {
          try {
            const tokenPayload = parseJwtToken(token)
            leaderId = tokenPayload.uid || tokenPayload.sub
          } catch (tokenError) {
            console.error('解析token失败:', tokenError)
            return
          }
        }
        
        if (!leaderId) return
        
        // 检查是否尝试邀请自己
        if (String(leaderId) === String(friendId)) {
          uni.showToast({ title: '不能邀请自己加入团队', icon: 'none' })
          return
        }
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/send-invitation`,
          method: 'POST',
          data: {
            taskId: Number(taskData.value?.taskId),
            leaderId: Number(leaderId),
            teamName: teamName.value.trim() || '临时团队',
            inviteeIds: [Number(friendId)]
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        if (response.data && response.data.success) {
          uni.showToast({
            title: '邀请发送成功',
            icon: 'success'
          })
          markUserAsInvited(friendId)
        } else {
          uni.showToast({
            title: response.data?.message || '发送邀请失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('发送邀请失败:', error)
        uni.showToast({
          title: '发送邀请失败',
          icon: 'none'
        })
      }
    };
    
    // 标记用户已邀请
    const markUserAsInvited = (friendId) => {
      invitedUsers.value.add(friendId)
    };
    
    const isUserInvited = (friendId) => {
      return invitedUsers.value.has(friendId)
    };
    
    // 处理团队名称输入
    const handleTeamNameInput = (e) => {
      console.log('团队名称输入事件:', e);
      console.log('输入值:', e.detail?.value || e.target?.value);
      console.log('editingTeamName当前值:', editingTeamName.value);
    };
    
    // 处理团队名称聚焦
    const handleTeamNameFocus = (e) => {
      console.log('团队名称输入框聚焦:', e);
      console.log('editingTeamName当前值:', editingTeamName.value);
    };
    
    // 测试设置团队名称
    const testSetTeamName = () => {
      console.log('测试设置团队名称');
      editingTeamName.value = '测试团队名称';
      console.log('设置后editingTeamName:', editingTeamName.value);
      uni.showToast({ title: '已设置测试团队名称', icon: 'success' });
    };
    
    // 保存团队名称
    const saveTeamName = async () => {
      if (!editingTeamName.value.trim()) {
        uni.showToast({ title: '请输入团队名称', icon: 'none' })
        return
      }
      
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      try {
        const token = uni.getStorageSync('accessToken')
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' })
          return
        }
        
        console.log('更新团队名称，参数:', {
          teamId: teamInfo.value.teamId,
          teamName: editingTeamName.value.trim()
        });
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/update-name`,
          method: 'POST',
          data: {
            teamId: Number(teamInfo.value.teamId),
            teamName: editingTeamName.value.trim()
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        console.log('更新团队名称响应:', response);
        
        if (response.data && response.data.success) {
          uni.showToast({ title: '团队名称更新成功', icon: 'success' })
          if (teamInfo.value) {
            teamInfo.value.teamName = editingTeamName.value.trim()
          }
        } else {
          uni.showToast({ title: response.data?.message || '更新失败', icon: 'none' })
        }
      } catch (error) {
        console.error('更新团队名称失败:', error)
        uni.showToast({ title: '更新团队名称失败', icon: 'none' })
      }
    };
    
    // 队长踢出队员
    const removeTeamMember = async (memberId) => {
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      try {
        const token = uni.getStorageSync('accessToken')
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' })
          return
        }
        
        const currentUser = uni.getStorageSync('userInfo')
        let leaderId = null
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid
        } else {
          try {
            const tokenPayload = parseJwtToken(token)
            leaderId = tokenPayload.uid || tokenPayload.sub
          } catch (tokenError) {
            console.error('解析token失败:', tokenError)
            uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
            return
          }
        }
        
        if (!leaderId) {
          uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
          return
        }
        
        console.log('踢出队员，参数:', {
          teamId: teamInfo.value.teamId,
          leaderId: leaderId,
          memberId: memberId
        })
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/remove-member`,
          method: 'POST',
          data: {
            teamId: teamInfo.value.teamId,
            leaderId: leaderId,
            memberId: memberId
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        console.log('踢出队员响应:', response)
        
        if (response.data && response.data.success) {
          uni.showToast({ title: '队员移除成功', icon: 'success' })
          // 重新加载团队信息
          await checkExistingTeam()
        } else {
          uni.showToast({ title: response.data?.message || '移除失败', icon: 'none' })
        }
      } catch (error) {
        console.error('踢出队员失败:', error)
        uni.showToast({ title: '踢出队员失败', icon: 'none' })
      }
    }
    
    // 发起踢人投票
    const initiateRemoveMemberVote = async (memberId) => {
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      // 显示输入原因的弹窗
      uni.showModal({
        title: '发起踢人投票',
        content: '请输入踢人原因',
        editable: true,
        placeholderText: '请输入踢人原因...',
        success: async (res) => {
          if (res.confirm && res.content) {
            try {
              const token = uni.getStorageSync('accessToken')
              if (!token) {
                uni.showToast({ title: '请先登录', icon: 'none' })
                return
              }
              
              const currentUser = uni.getStorageSync('userInfo')
              let initiatorId = null
              if (currentUser && currentUser.uid) {
                initiatorId = currentUser.uid
              } else {
                try {
                  const tokenPayload = parseJwtToken(token)
                  initiatorId = tokenPayload.uid || tokenPayload.sub
                } catch (tokenError) {
                  console.error('解析token失败:', tokenError)
                  uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
                  return
                }
              }
              
              if (!initiatorId) {
                uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
                return
              }
              
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/team-vote/initiate-remove-member`,
                method: 'POST',
                data: {
                  teamId: teamInfo.value.teamId,
                  initiatorId: initiatorId,
                  targetUserId: memberId,
                  reason: res.content
                },
                header: {
                  'Access-Token': `Bearer ${token}`,
                  'Content-Type': 'application/json'
                }
              })
              
              if (response.data && response.data.success) {
                uni.showToast({ title: '踢人投票已发起', icon: 'success' })
                // 重新加载团队信息
                await checkExistingTeam()
              } else {
                uni.showToast({ title: response.data?.message || '发起投票失败', icon: 'none' })
              }
            } catch (error) {
              console.error('发起踢人投票失败:', error)
              uni.showToast({ title: '操作失败，请重试', icon: 'none' })
            }
          }
        }
      })
    }
    
    // 发起弹劾队长投票
    const initiateImpeachLeaderVote = async () => {
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      // 显示输入原因的弹窗
      uni.showModal({
        title: '发起弹劾队长投票',
        content: '请输入弹劾原因',
        editable: true,
        placeholderText: '请输入弹劾原因...',
        success: async (res) => {
          if (res.confirm && res.content) {
            try {
              const token = uni.getStorageSync('accessToken')
              if (!token) {
                uni.showToast({ title: '请先登录', icon: 'none' })
                return
              }
              
              const currentUser = uni.getStorageSync('userInfo')
              let initiatorId = null
              if (currentUser && currentUser.uid) {
                initiatorId = currentUser.uid
              } else {
                try {
                  const tokenPayload = parseJwtToken(token)
                  initiatorId = tokenPayload.uid || tokenPayload.sub
                } catch (tokenError) {
                  console.error('解析token失败:', tokenError)
                  uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
                  return
                }
              }
              
              if (!initiatorId) {
                uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
                return
              }
              
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/team-vote/initiate-impeach-leader`,
                method: 'POST',
                data: {
                  teamId: teamInfo.value.teamId,
                  initiatorId: initiatorId,
                  reason: res.content
                },
                header: {
                  'Access-Token': `Bearer ${token}`,
                  'Content-Type': 'application/json'
                }
              })
              
              if (response.data && response.data.success) {
                uni.showToast({ title: '弹劾队长投票已发起', icon: 'success' })
                // 重新加载团队信息
                await checkExistingTeam()
              } else {
                uni.showToast({ title: response.data?.message || '发起投票失败', icon: 'none' })
              }
            } catch (error) {
              console.error('发起弹劾投票失败:', error)
              uni.showToast({ title: '操作失败，请重试', icon: 'none' })
            }
          }
        }
      })
    }
    
    // 队员退出团队
    const leaveTeam = async () => {
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      try {
        const token = uni.getStorageSync('accessToken')
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' })
          return
        }
        
        const currentUser = uni.getStorageSync('userInfo')
        let userId = null
        if (currentUser && currentUser.uid) {
          userId = currentUser.uid
        } else {
          try {
            const tokenPayload = parseJwtToken(token)
            userId = tokenPayload.uid || tokenPayload.sub
          } catch (tokenError) {
            console.error('解析token失败:', tokenError)
            uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
            return
          }
        }
        
        if (!userId) {
          uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
          return
        }
        
        console.log('退出团队，参数:', {
          teamId: teamInfo.value.teamId,
          userId: userId
        })
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/leave-team`,
          method: 'POST',
          data: {
            teamId: teamInfo.value.teamId,
            userId: userId
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        console.log('退出团队响应:', response)
        
        if (response.data && response.data.success) {
          uni.showToast({ title: '退出团队成功', icon: 'success' })
          
          // 退出团队后，立即创建新团队（用户成为队长）
          console.log('退出团队成功，开始创建新团队...')
          await createTeamImmediately()
          
          // 重新加载团队信息以确认新团队状态
          await checkExistingTeam()
        } else {
          uni.showToast({ title: response.data?.message || '退出失败', icon: 'none' })
        }
      } catch (error) {
        console.error('退出团队失败:', error)
        uni.showToast({ title: '退出团队失败', icon: 'none' })
      }
    }
    
    // 队长解散团队
    const dissolveTeam = async () => {
      if (!teamInfo.value || !teamInfo.value.teamId) {
        uni.showToast({ title: '团队信息不存在', icon: 'none' })
        return
      }
      
      try {
        // 确认解散
        const confirmResult = await new Promise((resolve) => {
          uni.showModal({
            title: '解散团队',
            content: '确定要解散当前团队吗？解散后所有成员将收到通知。',
            confirmText: '确定解散',
            cancelText: '取消',
            success: (res) => resolve(res.confirm)
          })
        })
        
        if (!confirmResult) {
          return
        }
        
        const token = uni.getStorageSync('accessToken')
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' })
          return
        }
        
        const currentUser = uni.getStorageSync('userInfo')
        let leaderId = null
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid
        } else {
          try {
            const tokenPayload = parseJwtToken(token)
            if (tokenPayload) {
              leaderId = tokenPayload.uid || tokenPayload.sub
            }
          } catch (tokenError) {
            console.error('解析token失败:', tokenError)
            uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
            return
          }
        }
        
        if (!leaderId) {
          uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' })
          return
        }
        
        console.log('解散团队，参数:', {
          teamId: teamInfo.value.teamId,
          leaderId: leaderId
        })
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/dissolve-team`,
          method: 'POST',
          data: {
            teamId: teamInfo.value.teamId,
            leaderId: leaderId
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        })
        
        console.log('解散团队响应:', response)
        
        if (response.data && response.data.success) {
          uni.showToast({ title: '团队解散成功', icon: 'success' })
          
          // 解散团队后，重置团队信息
          teamInfo.value = null
          isTeamLeader.value = false
          teamMode.value = false
          
          // 重新检查团队状态
          await checkExistingTeam()
        } else {
          uni.showToast({ title: response.data?.message || '解散失败', icon: 'none' })
        }
      } catch (error) {
        console.error('解散团队失败:', error)
        uni.showToast({ title: '解散团队失败', icon: 'none' })
      }
    }
    
    const openTeamInvitationModal = async () => {
      try {
        // 先创建团队和队长的TeamMember记录
        const token = uni.getStorageSync('accessToken');
        const currentUser = uni.getStorageSync('userInfo');
        
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' });
          return;
        }
        
        let leaderId = null;
        if (currentUser && currentUser.uid) {
          leaderId = currentUser.uid;
        } else {
          try {
            const tokenPayload = parseJwtToken(token);
            if (tokenPayload) {
              leaderId = tokenPayload.uid || tokenPayload.sub;
            }
          } catch (tokenError) {
            console.error('解析token失败:', tokenError);
            uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' });
            return;
          }
        }
        
        if (!leaderId) {
          uni.showToast({ title: '用户信息获取失败，请重新登录', icon: 'none' });
          return;
        }
        
        console.log('开始创建团队，参数:', {
          taskId: taskData.value?.taskId,
          leaderId: leaderId,
          teamName: '临时团队'
        });
        
        // 创建团队和队长的TeamMember记录
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/create-invitation`,
          method: 'POST',
          data: {
            taskId: Number(taskData.value?.taskId),
            leaderId: Number(leaderId),
            teamName: '临时团队',
            inviteeIds: [] // 暂时不邀请任何人，只创建团队
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        console.log('团队创建响应:', response);
        
        if (response.data && response.data.success) {
          // 根据是否已存在显示不同消息
          if (response.data.isExisting) {
            uni.showToast({ title: '团队已存在', icon: 'success' });
          } else {
            uni.showToast({ title: '已为您创建团队', icon: 'success' });
          }
          
          // 保存团队信息到响应式数据
          if (response.data.teamInfo) {
            teamInfo.value = response.data.teamInfo;
            console.log('团队信息:', teamInfo.value);
          }
          
          // 打开邀请模态框
          showTeamInvitationModal.value = true;
        } else {
          uni.showToast({ title: response.data?.message || '团队创建失败', icon: 'none' });
        }
      } catch (error) {
        console.error('创建团队失败:', error);
        uni.showToast({ title: '创建团队失败', icon: 'none' });
      }
    };
    
    const closeTeamInvitationModal = () => {
      showTeamInvitationModal.value = false;
    };
    
    const handleTeamInvitationConfirm = (teamId) => {
      console.log('团队邀请创建成功，团队ID:', teamId);
      // 可以在这里刷新团队信息或显示成功提示
      loadTeamInfo();
    };
    
    const handleUpdateTeamName = (newTeamName) => {
      if (teamInfo.value) {
        teamInfo.value.teamName = newTeamName;
      }
    };
    
    const loadTeamInfo = async () => {
      try {
        const token = uni.getStorageSync('accessToken');
        const currentUser = uni.getStorageSync('userInfo');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        
        if (!taskIdVal) return;
        
        // 这里需要根据任务ID查找团队信息
        // 暂时先不实现，等后端API完善后再添加
        console.log('加载团队信息，任务ID:', taskIdVal);
      } catch (error) {
        console.error('加载团队信息失败:', error);
      }
    };
    
    // 更新团队名称
    const updateTeamName = async (teamId, newTeamName) => {
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' });
          return;
        }
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/team/update-name`,
          method: 'POST',
          data: {
            teamId: teamId,
            teamName: newTeamName
          },
          header: {
            'Access-Token': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        if (response.data && response.data.success) {
          uni.showToast({ title: '团队名称更新成功', icon: 'success' });
          // 更新本地团队信息
          if (teamInfo.value) {
            teamInfo.value.teamName = newTeamName;
          }
        } else {
          uni.showToast({ title: response.data?.message || '更新失败', icon: 'none' });
        }
      } catch (error) {
        console.error('更新团队名称失败:', error);
        uni.showToast({ title: '更新团队名称失败', icon: 'none' });
      }
    };
    
    // 处理驳回申请完成
    const handleRejectCompletion = () => {
      console.log('点击驳回申请按钮');
      uni.showModal({
        title: '驳回申请',
        content: '确定要驳回这个完成申请吗？',
        success: async (res) => {
          if (res.confirm) {
            console.log('用户确认驳回申请');
            console.log('completePreview.value:', completePreview.value);
            console.log('messageId:', completePreview.value?.messageId);
            console.log('proof:', completePreview.value?.proof);
            try {
              const token = uni.getStorageSync('accessToken');
              const requestData = `approve=false&proof=${encodeURIComponent(completePreview.value?.proof || '')}&id=${completePreview.value?.messageId}`;
              console.log('请求数据:', requestData);
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/message/handle-task-completion`,
                method: 'POST',
                header: { 
                  'Access-Token': `Bearer ${token}`,
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: requestData
              });
              
              console.log('驳回申请响应:', response);
              
              if (response.statusCode === 200) {
                uni.showToast({ title: '已驳回申请', icon: 'success' });
                closeCompletePreview();
                // 刷新任务数据
                await fetchTaskData();
              } else {
                uni.showToast({ title: '驳回失败', icon: 'error' });
              }
            } catch (error) {
              console.error('驳回申请失败:', error);
              uni.showToast({ title: '驳回失败', icon: 'error' });
            }
          }
        }
      });
    };
    
    // 处理同意申请完成
    const handleApproveCompletion = () => {
      console.log('点击同意申请按钮');
      uni.showModal({
        title: '同意申请',
        content: '确定要同意这个完成申请吗？',
        success: async (res) => {
          if (res.confirm) {
            console.log('用户确认同意申请');
            console.log('completePreview.value:', completePreview.value);
            console.log('messageId:', completePreview.value?.messageId);
            console.log('proof:', completePreview.value?.proof);
            try {
              const token = uni.getStorageSync('accessToken');
              const requestData = `approve=true&proof=${encodeURIComponent(completePreview.value?.proof || '')}&id=${completePreview.value?.messageId}`;
              console.log('请求数据:', requestData);
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/message/handle-task-completion`,
                method: 'POST',
                header: { 
                  'Access-Token': `Bearer ${token}`,
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: requestData
              });
              
              console.log('同意申请响应:', response);
              
              if (response.statusCode === 200) {
                uni.showToast({ title: '已同意申请', icon: 'success' });
                closeCompletePreview();
                // 刷新任务数据
                await fetchTaskData();
              } else {
                uni.showToast({ title: '同意失败', icon: 'error' });
              }
            } catch (error) {
              console.error('同意申请失败:', error);
              uni.showToast({ title: '同意失败', icon: 'error' });
            }
          }
        }
      });
    };
    const applyCompleteTask = async () => {
      if (!completeProof.value) {
        uni.showToast({ title: '请填写完成证明', icon: 'none' });
        return;
      }
      uni.showLoading({ title: '提交申请中...' });
      let isValid = true;
      taskData.value?.badges.forEach(badge => {
        if (calculateTotalPercentage(badge.id) !== 100) {
          isValid = false;
        }
      });
      if (!isValid) {
        uni.showToast({ title: '每个铭牌的贡献比例总和必须为100%', icon: 'none' });
        return;
      }
      const badges = taskData.value?.badges.map(badge => ({
        badgeId: badge.id,
        contributors: badgeContributors[badge.id]
      }));
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) {
          uni.showToast({ title: '请先登录', icon: 'none' });
          return;
        }
        
        const requestUrl = `${NETWORK_CONFIG.API_BASE_URL}/task/applycomplete?taskId=${taskData.value?.taskId}&proof=${encodeURIComponent(completeProof.value)}`;
        const requestData = { badges: badges };
        
        console.log('申请完成任务请求参数:', {
          url: requestUrl,
          taskId: taskData.value?.taskId,
          proof: completeProof.value,
          badges: badges,
          requestData: requestData
        });
        
        const response = await uni.request({
          url: requestUrl,
          method: 'POST',
          data: requestData,
          header: {
            'Content-Type': 'application/json',
            'Access-Token': `Bearer ${token}`
          }
        });
        if (response.statusCode === 200) {
          uni.hideLoading();
          uni.showToast({ title: '申请完成任务成功！', icon: 'success' });
          // 标记为已申请，并刷新待处理状态
          hasAppliedCompletion.value = true;
          await checkPendingMessages();
        } else {
          throw new Error(response.data?.message || '申请失败');
        }
      } catch (e) {
        uni.hideLoading();
        uni.showToast({ title: e.message || '申请失败', icon: 'none' });
      }
    };
    const cancelCompletionByTask = async () => {
      uni.showModal({
        title: '撤回完成申请',
        content: '确认撤回已提交的完成申请吗？',
        success: async (res) => {
          if (!res.confirm) return;
          try {
            const token = uni.getStorageSync('accessToken');
            if (!token) throw new Error('请先登录');
            const taskIdVal = taskData.value?.taskId || task.value?.taskId;
            if (!taskIdVal) throw new Error('任务 ID 缺失');
            const response = await uni.request({
              url: `${NETWORK_CONFIG.API_BASE_URL}/message/cancel-task-completion-by-task?taskId=${taskIdVal}`,
              method: 'POST',
              header: { 'Access-Token': `Bearer ${token}` }
            });
            if (response.statusCode === 200) {
              uni.showToast({ title: '已撤回完成申请', icon: 'success' });
              hasAppliedCompletion.value = false;
              await checkPendingMessages();
            } else if (response.statusCode === 404) {
              // 回退：查找消息ID并按ID撤回
              const respId = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/message/completion-apply/message-id?taskId=${taskIdVal}`,
                method: 'GET',
                header: { 'Access-Token': `Bearer ${token}` }
              });
              if (respId.statusCode === 200 && respId.data && respId.data.messageId) {
                const msgId = respId.data.messageId;
                const respCancel = await uni.request({
                  url: `${NETWORK_CONFIG.API_BASE_URL}/message/cancel-task-completion?id=${msgId}`,
                  method: 'POST',
                  header: { 'Access-Token': `Bearer ${token}` }
                });
                if (respCancel.statusCode === 200) {
                  uni.showToast({ title: '已撤回完成申请', icon: 'success' });
                  hasAppliedCompletion.value = false;
                  await checkPendingMessages();
                } else {
                  throw new Error(respCancel.data?.message || '撤回失败');
                }
              } else {
                throw new Error(respId.data?.message || '未找到申请消息');
              }
            } else {
              throw new Error(response.data?.message || '撤回失败');
            }
          } catch (e) {
            uni.showToast({ title: e.message || '撤回失败', icon: 'none' });
          }
        }
      });
    };
    const handleError = (err, context) => {
      uni.showToast({ title: '操作失败，请稍后重试', icon: 'none', duration: 2000 });
    };
    uni.onError((err) => { handleError(err, '全局'); });
    const debounce = (func, wait) => {
      let timeout;
      return function executedFunction(...args) {
        const later = () => {
          clearTimeout(timeout);
          func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
      };
    };
    const debouncedValidateTotalPercentage = debounce(validateTotalPercentage, 300);
    const allBadgesValid = computed(() => {
      if (!taskData.value?.badges) return false;
      return taskData.value.badges.every(b => calculateTotalPercentage(b.id) === 100);
    });
    const canSubmitComplete = computed(() => {
      return !!completeProof.value && allBadgesValid.value;
    });
    const goToApplyComplete = () => {
      scrollIntoView.value = 'apply-complete-anchor';
      // 小程序场景下，再触发一次以确保滚动
      setTimeout(() => { scrollIntoView.value = ''; }, 300);
    };
    // 数字格式化函数 - 使用千分位分隔符，始终显示完整数字
    const formatNumber = (num) => {
      if (num === null || num === undefined || isNaN(num)) {
        return '0';
      }
      const number = Number(num);
      return number.toLocaleString();
    };
    // 百分比格式化函数
    const formatPercentage = (num) => {
      if (num === null || num === undefined || isNaN(num)) {
        return '0';
      }
      return Number(num).toLocaleString();
    };
    // 字符数格式化函数 - 不使用中文单位
    const formatCharacterCount = (num) => {
      if (num === null || num === undefined || isNaN(num)) {
        return '0';
      }
      return Number(num).toLocaleString();
    };
    // 日期时间格式化函数
    const formatDateTime = (timestamp) => {
      if (!timestamp) return '未知时间';
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    };

    // 工作记录相关状态和方法
    const workTimes = ref([]);
    const workLoading = ref(false);
    const workError = ref(null);
    const showWorkControls = computed(() => {
      if (taskData.value?.status !== '2') return false; // 只在进行中时显示工作控制
      
      const currentUserId = userStore.profile?.uid;
      if (!currentUserId) return false;
      
      // 如果有队伍，检查是否是队长或队员
      if (teamInfo.value && teamInfo.value.members && teamInfo.value.members.length > 0) {
        const isTeamMember = teamInfo.value.members.some(member => 
          String(member.userId) === String(currentUserId)
        );
        return isTeamMember;
      }
      
      // 如果没有队伍，检查是否是challenger
      const isChallenger = taskData.value?.challengerId && 
        String(taskData.value.challengerId) === String(currentUserId);
      return isChallenger;
    });
    const hasOngoingWork = computed(() => {
      if (!Array.isArray(workTimes.value)) return false;
      return workTimes.value.some(wt => {
        const endVal = wt?.endTime ?? wt?.end_at ?? wt?.end;
        return endVal === null || endVal === undefined;
      });
    });
    const startWorking = ref(false);
    const endWorking = ref(false);

    const normalizeArrayResponse = (resData) => {
      if (Array.isArray(resData)) return resData;
      if (resData && Array.isArray(resData.data)) return resData.data;
      if (resData && Array.isArray(resData.result)) return resData.result;
      if (resData && Array.isArray(resData.payload)) return resData.payload;
      return [];
    };

    const fetchWorkTimes = async () => {
      workLoading.value = true;
      workError.value = null;
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) throw new Error('请先登录');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId || taskId;
        // 若仍然拿不到ID，安静退出，不提示错误（等详情加载完后再拉取）
        if (!taskIdVal) { return; }
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/work/times?taskId=${taskIdVal}`,
          method: 'GET',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        if (response.statusCode === 200 && response.data !== undefined) {
          workTimes.value = normalizeArrayResponse(response.data);
        } else {
          throw new Error(response.data?.message || '加载工作记录失败');
        }
      } catch (e) {
        // 仅在有明确错误时提示；任务ID缺失等初始化过程不提示
        if (e && e.message && !/任务 ID 缺失/.test(e.message)) {
          workError.value = e.message;
        }
      } finally {
        workLoading.value = false;
      }
    };

    const startWork = async () => {
      if (startWorking.value) return;
      startWorking.value = true;
      uni.showLoading({ title: '开始中...' });
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) throw new Error('请先登录');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) throw new Error('任务 ID 缺失');
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/work/start?taskId=${taskIdVal}`,
          method: 'POST',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        if (response.statusCode === 200 && response.data) {
          uni.hideLoading();
          uni.showToast({ title: '开始工作成功！', icon: 'success' });
          await fetchWorkTimes(); // 刷新工作记录
        } else {
          throw new Error(response.data?.message || '开始工作失败');
        }
      } catch (e) {
        uni.hideLoading();
        uni.showToast({ title: e.message || '开始工作失败', icon: 'none' });
      } finally {
        startWorking.value = false;
      }
    };

    const endWork = async () => {
      if (endWorking.value) return;
      endWorking.value = true;
      uni.showLoading({ title: '结束中...' });
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) throw new Error('请先登录');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) throw new Error('任务 ID 缺失');
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/work/end?taskId=${taskIdVal}`,
          method: 'POST',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        if (response.statusCode === 200 && response.data) {
          uni.hideLoading();
          uni.showToast({ title: '结束工作成功！', icon: 'success' });
          await fetchWorkTimes(); // 刷新工作记录
        } else {
          throw new Error(response.data?.message || '结束工作失败');
        }
      } catch (e) {
        uni.hideLoading();
        uni.showToast({ title: e.message || '结束工作失败', icon: 'none' });
      } finally {
        endWorking.value = false;
      }
    };

    // 任务取消相关方法
    const cancelTaskApply = async () => {
      uni.showModal({
        title: '确认撤回申请',
        content: '确定要撤回对任务《' + taskData.value?.title + '》的申请吗？',
        success: async (res) => {
          if (res.confirm) {
            uni.showLoading({ title: '撤回申请中...' });
            try {
              const token = uni.getStorageSync('accessToken');
              if (!token) throw new Error('请先登录');
              const taskIdVal = taskData.value?.taskId || task.value?.taskId;
              if (!taskIdVal) throw new Error('任务 ID 缺失');
              
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/task/cancel-apply?taskId=${taskIdVal}`,
                method: 'POST',
                header: { 'Access-Token': `Bearer ${token}` },
                timeout: 8000
              });
              
              if (response.statusCode === 200) {
                uni.hideLoading();
                uni.showToast({ title: '申请已撤回', icon: 'success' });
                await fetchTaskDetail(taskIdVal); // 刷新任务详情
                await checkUserApplied(); // 重新检查用户申请状态
              } else {
                throw new Error(response.data?.message || '撤回申请失败');
              }
            } catch (e) {
              uni.hideLoading();
              uni.showToast({ title: e.message || '撤回申请失败', icon: 'none' });
            }
          }
        }
      });
    };

    const openCancelRequestModal = () => {
      cancelReason.value = '';
      showCancelRequestModal.value = true;
    };

    const closeCancelRequestModal = () => {
      showCancelRequestModal.value = false;
      cancelReason.value = '';
    };

    const submitCancelRequest = async () => {
      if (!cancelReason.value.trim()) {
        uni.showToast({ title: '请输入取消原因', icon: 'none' });
        return;
      }
      
      uni.showLoading({ title: '提交申请中...' });
      try {
        const token = uni.getStorageSync('accessToken');
        if (!token) throw new Error('请先登录');
        const taskIdVal = taskData.value?.taskId || task.value?.taskId;
        if (!taskIdVal) throw new Error('任务 ID 缺失');
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/task/request-cancel?taskId=${taskIdVal}&reason=${encodeURIComponent(cancelReason.value)}`,
          method: 'POST',
          header: { 'Access-Token': `Bearer ${token}` },
          timeout: 8000
        });
        
        if (response.statusCode === 200) {
          uni.hideLoading();
          uni.showToast({ title: '取消申请已发送', icon: 'success' });
          closeCancelRequestModal();
          await fetchTaskDetail(taskIdVal); // 刷新任务详情
        } else {
          throw new Error(response.data?.message || '提交申请失败');
        }
      } catch (e) {
        uni.hideLoading();
        uni.showToast({ title: e.message || '提交申请失败', icon: 'none' });
      }
    };

    const deleteTask = async () => {
      uni.showModal({
        title: '确认删除任务',
        content: '确定要删除任务《' + taskData.value?.title + '》吗？删除后无法恢复。',
        success: async (res) => {
          if (res.confirm) {
            uni.showLoading({ title: '删除中...' });
            try {
              const token = uni.getStorageSync('accessToken');
              if (!token) throw new Error('请先登录');
              const taskIdVal = taskData.value?.taskId || task.value?.taskId;
              if (!taskIdVal) throw new Error('任务 ID 缺失');
              
              const response = await uni.request({
                url: `${NETWORK_CONFIG.API_BASE_URL}/task/delete?taskId=${taskIdVal}`,
                method: 'DELETE',
                header: { 'Access-Token': `Bearer ${token}` },
                timeout: 8000
              });
              
              if (response.statusCode === 200) {
                uni.hideLoading();
                uni.showToast({ title: '任务已删除', icon: 'success' });
                // 返回上一页
                setTimeout(() => {
                  uni.navigateBack();
                }, 1500);
              } else {
                throw new Error(response.data?.message || '删除任务失败');
              }
            } catch (e) {
              uni.hideLoading();
              uni.showToast({ title: e.message || '删除任务失败', icon: 'none' });
            }
          }
        }
      });
    };

    // 跳转到消息中心
    const goToMessages = () => {
      uni.navigateTo({
        url: '/pages/message/index'
      });
    };

    // 处理任务取消申请被同意的事件
    const handleTaskCancelApproved = async (eventData) => {
      console.log('收到任务取消申请被同意的事件:', eventData);
      if (eventData.taskId && String(eventData.taskId) === String(taskId)) {
        console.log('当前任务被取消，刷新任务详情');
        // 刷新任务详情
        await fetchTaskDetail(taskId);
        // 重新检查用户申请状态
        await checkUserApplied();
        // 重新检查待处理消息
        await checkPendingMessages();
        uni.showToast({ 
          title: '任务已取消，状态已更新', 
          icon: 'success' 
        });
      }
    };

    // 处理团队成员变更消息
    const handleTeamMemberChange = async (message) => {
      console.log('收到团队成员变更消息:', message);
      
      // 检查是否与当前任务相关
      if (message.taskId && String(message.taskId) === String(taskId)) {
        console.log('当前任务的团队成员发生变更，刷新团队信息');
        
        // 重新获取团队信息
        await checkExistingTeam();
        
        // 显示通知
        if (message.action === 'join') {
          uni.showToast({ 
            title: '有新成员加入团队', 
            icon: 'success',
            duration: 2000
          });
        } else if (message.action === 'leave') {
          uni.showToast({ 
            title: '有成员离开团队', 
            icon: 'none',
            duration: 2000
          });
        } else if (message.action === 'remove') {
          uni.showToast({ 
            title: '有成员被移除', 
            icon: 'none',
            duration: 2000
          });
        }
      }
    };

    // 处理团队邀请消息
    const handleTeamInvitationMessage = async (message) => {
      console.log('收到团队邀请消息:', message);
      
      // 检查是否与当前任务相关
      if (message.taskId && String(message.taskId) === String(taskId)) {
        console.log('当前任务收到新的团队邀请');
        
        // 重新获取团队信息以更新待处理邀请
        await checkExistingTeam();
        
        uni.showToast({ 
          title: '收到新的团队邀请', 
          icon: 'success',
          duration: 2000
        });
      }
    };

    onLoad(async (options) => {
      uni.setNavigationBarTitle({ title: '任务详情' });
      taskId = options.id || options.taskId || options.task_id;
      const messageId = options.messageId;
      const showCompletion = options.showCompletion === 'true';
      console.log('页面加载参数:', { options, taskId, messageId, showCompletion });
      
      // 确保用户信息已初始化
      if (!userStore.isInitialized) {
        console.log('用户信息未初始化，开始初始化...');
        await userStore.initialize();
      }
      
      console.log('当前用户信息:', { 
        userStore: userStore, 
        profile: userStore.profile,
        uid: userStore.profile?.uid,
        currentUserId: currentUserId.value,
        isInitialized: userStore.isInitialized
      });
      
      if (taskId) {
        await fetchTaskDetail(taskId);
        await fetchWorkTimes(); // 详情完成后再拉取工作记录，避免任务ID为空
        await checkPendingMessages(); // 检查待处理消息
        await checkUserApplied(); // 检查用户申请状态
        await checkExistingTeam(); // 检查是否已有团队，决定是否默认勾选组队模式
        
        // 如果是从消息跳转过来且需要显示申请完成详情
        if (showCompletion && messageId) {
          console.log('从消息跳转过来，显示申请完成详情，消息ID:', messageId);
          // 延迟一点时间确保任务数据已加载完成
          setTimeout(async () => {
            await fetchApplyCompletePreview();
            showCompletePreview.value = true;
          }, 500);
        }
      } else {
        error.value = "任务 ID 缺失";
        loading.value = false;
      }
      
      // 监听任务取消申请被同意的事件
      uni.$on('task-cancel-approved', handleTaskCancelApproved);
      
      // 监听WebSocket消息
      uni.$on('team-member-change', handleTeamMemberChange);
      uni.$on('team-invitation', handleTeamInvitationMessage);
    });
    onShow(() => {});
    onHide(() => {});
    onUnload(() => {
      // 清理事件监听器
      uni.$off('task-cancel-approved', handleTaskCancelApproved);
      uni.$off('team-member-change', handleTeamMemberChange);
      uni.$off('team-invitation', handleTeamInvitationMessage);
    });
    return {
      task,
      loading,
      error,
      scrollIntoView,
      defaultAvatar,
      getStatusText,
      getBadgeNameByAssociation,
      moveToTaskLocation,
      applyForTask,
      challengeContent,
      showApplyCompleteBtn,
      completeProof,
      canSubmitComplete,
      goToApplyComplete,
      showCompletePreview,
      previewLoading,
      previewError,
      completePreview,
      viewApplyCompleteInfo,
      closeCompletePreview,
      badgeContributions,
      applyCompleteTask,
      currentUserId,
      isPublisherAndInProgress,
      badgeContributors,
      addContributor,
      removeContributor,
      selectContributor,
      clearContributor,
      getTeamMembersForBadge,
      calculateTotalPercentage,
      validateTotalPercentage,
      debouncedValidateTotalPercentage,
      userStore,
      mapMarkers,
      refreshing,
      onRefresh,
      handleError,
      taskData,
      currentUserId,
      formatNumber,
      formatPercentage,
      formatCharacterCount,
      workTimes,
      workLoading,
      workError,
      showWorkControls,
      hasOngoingWork,
      startWorking,
      endWorking,
      startWork,
      endWork,
      formatDateTime,
      // 任务取消相关
      showTaskActions,
      canCancelApply,
      canRequestCancel,
      canDeleteTask,
      showCancelRequestModal,
      cancelReason,
      cancelTaskApply,
      openCancelRequestModal,
      closeCancelRequestModal,
      submitCancelRequest,
      hasAppliedCompletion,
      cancelCompletionByTask,
      deleteTask,
      // 消息相关
      hasPendingMessages,
      pendingMessageTypes,
      checkPendingMessages,
      hasUserApplied,
      checkUserApplied,
      goToMessages,
      // 用户信息相关
      getUserAvatar,
      getSafeUserAvatar,
      getUserName,
      handleAvatarError,
      handleTaskAvatarError,
      getAvatarUrl,
      handleRejectCompletion,
      handleApproveCompletion,
      // 组队相关
      showTeamInvitationModal,
      teamInfo,
      teamMembers,
      teamStatus,
      openTeamInvitationModal,
      closeTeamInvitationModal,
      handleTeamInvitationConfirm,
      loadTeamInfo,
      updateTeamName,
      handleUpdateTeamName,
      // 新的组队模式相关
      teamMode,
      teamName,
      editingTeamName,
      userInfoCache,
      canShowTeamMode,
      canShowBadgeAllocation,
      searchKeyword,
      searchResults,
      searchLoading,
      searchError,
      invitedUsers,
      toggleTeamMode,
      checkExistingTeam,
      handleApplication,
      handleTeamApplication,
      getTeamStatusText,
      formatTime,
      getUserInfo,
      getUserNickname,
      getUserAvatar,
      searchFriends,
      inviteSingleUser,
      markUserAsInvited,
      isUserInvited,
      saveTeamName,
      isTeamLeader,
      removeTeamMember,
      leaveTeam,
      dissolveTeam
    };
  }
};
</script>

<style scoped>
/* 主滚动容器 - 撑满整个屏幕 */
.scroll-container {
  height: 100vh;
  width: 100vw;
  background: #f5f5f5;
  margin: 0 auto;
}

/* 加载和错误状态 */
.loading,
.error {
  text-align: center;
  margin: 20px 0;
  font-size: 1.2rem;
  color: #d63031;
}

/* 头部卡片 - 包含头像、标题、状态、发布者、时间信息 */
.task-header {
  width: calc(100vw - 64px);
  margin: 10px auto;
  display: flex;
  align-items: center;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

/* 用户头像 */
.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 20px;
  border: 2px solid #74b9ff;
  object-fit: cover;
  overflow: hidden;
}

/* 头部主要内容区域 */
.header-main {
  flex: 1;
  margin-left: 20px;
  text-align: left;
}

/* 标题和状态标签行 */
.header-title-row {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

/* 任务标题 */
.task-title {
  font-size: 18px;
  font-weight: 700;
  color: #2c3e50;
  flex: 1;
  line-height: 1.4;
  margin: 0;
  letter-spacing: -0.2px;
  text-align: left;
}

/* 状态标签 */
.status-tag {
  background-color: #ff9f43;
  color: white;
  padding: 6px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  margin-left: 12px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  box-shadow: 0 2px 4px rgba(255, 159, 67, 0.3);
}

/* 发布信息 */
.task-publisher,
.task-time,
.task-deadline {
  font-size: 13px;
  color: #7f8c8d;
  margin-bottom: 2px;
  font-weight: 400;
  letter-spacing: 0.1px;
  text-align: left;
}

/* 任务详情卡片 - 包含描述、金额、状态等信息 */
.task-body {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  line-height: 1.6;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

/* 任务信息行 - 标签和值的组合 */
.task-info-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 12px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.task-info-row:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

/* 信息标签 */
.task-info-label {
  font-weight: 600;
  color: #34495e;
  min-width: 80px;
  flex-shrink: 0;
  margin-right: 16px;
  font-size: 14px;
  letter-spacing: 0.3px;
}

/* 信息值 */
.task-info-value {
  color: #2c3e50;
  flex: 1;
  word-break: break-word;
  line-height: 1.6;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.1px;
  text-align: left;
}

/* 地图容器 */
.map-container {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  text-align: center;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
  padding: 16px;
}

/* 地图组件 */
.map {
  width: 100%;
  height: 300px;
  border-radius: 8px;
  border: 2px solid #74b9ff;
}

/* 定位按钮 */
.move-to-task-btn {
  width: 100%;
  background: #74b9ff;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  line-height: 1.4;
  text-align: center;
  margin: 8px 0;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-sizing: border-box;
  font-weight: 600;
  letter-spacing: 0.3px;
}

/* 徽章卡片 - 完成任务获得的荣誉 */
.task-badges {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

/* 徽章标题 */
.badge-title {
  font-size: 16px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 12px;
  letter-spacing: -0.1px;
}

/* 单个徽章 */
.badge {
  border: 1px solid #dfe6e9;
  padding: 12px;
  margin-bottom: 12px;
  border-radius: 12px;
  background: linear-gradient(135deg, #ffeaa7, #fab1a0);
  color: #2c3e50;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
  letter-spacing: 0.2px;
}

/* 徽章标签 */
.badge-label {
  font-weight: 600;
  color: #2c3e50;
  letter-spacing: 0.3px;
}

/* 申请挑战任务区域 */
.challenge-section {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

/* 申请完成任务区域 */
.apply-complete-section {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

/* 申请完成标题 */
.apply-complete-title {
  font-size: 16px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 16px;
  letter-spacing: -0.1px;
}

/* 表单分组 */
.form-group {
  margin-bottom: 20px;
}

/* 表单标签 */
.form-group text {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #34495e;
  font-size: 14px;
  letter-spacing: 0.3px;
}

/* 徽章表单 */
.badge-form {
  background: #fff;
  padding: 20px;
  border-radius: 16px;
  margin-bottom: 18px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* 让“铭牌描述”输入与下方 UID/角色/贡献比例保持完全一致的外观
   注意：放宽选择器，适配编译后层级（不一定是直系子元素） */
.badge-form input:not([type="hidden"]):not(.wx-hidden),
.badge-form textarea,
.badge-form > view:not(.contributor-form) input:not([type="hidden"]),
.badge-form input.badge-desc,
.badge-form textarea.badge-desc {
  width: 100%;
  padding: 14px 18px !important;
  border: 1px solid #ddd !important;
  border-radius: 16px !important;
  font-size: 16px !important;
  background: #ffffff !important;
  box-sizing: border-box !important;
  margin: 8px 0 12px !important;
  line-height: 1.5 !important;
  letter-spacing: 0.1px !important;
  min-height: 50px !important;
  color: #2c3e50 !important;
  -webkit-text-fill-color: #2c3e50 !important;
  caret-color: #2c3e50 !important;
  -webkit-appearance: none !important;
  appearance: none !important;
  color-scheme: light !important;
}

/* 贡献者表单 */
.contributor-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 18px;
  padding: 14px 12px 16px 12px;
  background: #f8f9fa;
  border-radius: 16px;
  border: 1px solid #e2e8f0;
  /* 某些 iOS 下祖先层的 transform/opacity 会影响文本渲染，强制自身图层 */
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  /* 触控命中更友好 */
  min-height: 48px;
}

/* 贡献者表单标签 */
.contributor-form text {
  font-size: 14px;
  color: #556;
  margin-bottom: 6px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

/* 贡献者选择器容器 */
.contributor-selector {
  width: 100%;
  margin-bottom: 6px;
}

/* 当前用户显示 */
.current-user-display {
  padding: 12px 16px;
  background: #e8f4fd;
  border: 1px solid #b3d9ff;
  border-radius: 8px;
  text-align: center;
}

.current-user-display text {
  color: #1976d2;
  font-weight: 500;
  font-size: 14px;
}

/* 输入框和文本域 */
input,
textarea,
.challenge-content,
.complete-proof-textarea {
  width: 100%;
  padding: 14px 18px;
  border: 1px solid #ddd;
  border-radius: 16px;
  /* iOS 小于16px聚焦会自动放大，易引发渲染异常 */
  font-size: 16px;
  background: #ffffff;
  box-sizing: border-box;
  margin: 4px 0;
  resize: none;
  transition: all 0.3s ease;
  line-height: 1.5;
  letter-spacing: 0.1px;
  /* 明确指定文本与光标颜色，修复 iOS 深色模式文字不可见 */
  color: #2c3e50 !important;
  -webkit-text-fill-color: #2c3e50 !important;
  caret-color: #2c3e50;
  -webkit-appearance: none;
  appearance: none;
  /* 禁用系统暗色自动反色 */
  color-scheme: light;
}

/* 放大“UID/角色/贡献比例”三个输入框尺寸 */
.contributor-form input,
.contributor-form textarea {
  min-height: 50px;
  line-height: 1.6;
  padding: 14px 18px;
  border-radius: 16px;
}

/* 输入框焦点状态 */
input:focus,
textarea:focus,
.challenge-content:focus,
.complete-proof-textarea:focus {
  border-color: #6c5ce7;
  outline: none;
  box-shadow: 0 0 0 2px rgba(108, 92, 231, 0.1);
  /* 避免某些 iOS 机型在 transform 下触发文本绘制异常 */
  /* transform: translateY(-1px); */
  background: #ffffff !important;
  color: #2c3e50 !important;
  -webkit-text-fill-color: #2c3e50 !important;
  /* 让焦点态也更容易点击 */
  min-height: 50px;
}

/* 占位符颜色，防止深色模式下过浅 */
input::placeholder,
textarea::placeholder {
  color: #95a5a6 !important;
  opacity: 1;
}

/* 禁用状态 */
input[disabled],
textarea[disabled] {
  background: #f5f5f5;
  color: #999;
}

/* 挑战内容容器 */
.challenge-content-container {
  position: relative;
  width: 100%;
  margin-top: 1rem;
}

/* 挑战内容文本域 */
.challenge-content {
  min-height: 96px;
}

/* 完成证明容器 */
.complete-proof-container {
  position: relative;
  width: 100%;
  margin-top: 1rem;
}

/* 完成证明文本域 */
.complete-proof-textarea {
  min-height: 112px;
}

/* 字符计数器 */
.character-counter {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  font-size: 12px;
  color: #7f8c8d;
  background: rgba(255, 255, 255, 0.95);
  padding: 4px 8px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  font-weight: 500;
  letter-spacing: 0.2px;
}

/* 百分比总计 */
.percentage-total {
  margin: 12px 0;
  font-weight: 600;
  font-size: 14px;
  color: #2c3e50;
  letter-spacing: 0.2px;
}

/* 错误文本 */
.error-text {
  color: #e74c3c;
  font-weight: 500;
  font-size: 13px;
  letter-spacing: 0.1px;
}

/* 按钮基础样式 */
.challenge-btn,
.apply-complete-btn,
.add-btn,
.remove-btn {
  width: 100%;
  background: #74b9ff;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  line-height: 1.4;
  text-align: center;
  margin: 8px 0;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-sizing: border-box;
  font-weight: 600;
  letter-spacing: 0.3px;
}

/* 按钮悬停效果 */
.move-to-task-btn:hover,
.challenge-btn:hover,
.apply-complete-btn:hover,
.add-btn:hover {
  background: #0984e3;
  /* iOS 输入聚焦期间移除位移动画，避免文本闪烁 */
  /* transform: translateY(-2px); */
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 挑战按钮特殊样式 */
.challenge-btn {
  background: #6c5ce7;
  font-weight: bold;
  margin-top: 15px;
  box-shadow: 0 2px 8px rgba(108, 92, 231, 0.3);
}

/* 申请完成按钮特殊样式 */
.apply-complete-btn {
  background: #00b894;
  font-weight: bold;
  margin-top: 15px;
  box-shadow: 0 2px 8px rgba(0, 184, 148, 0.3);
}

/* 删除按钮特殊样式 */
.remove-btn {
  background: #ff7675;
  padding: 8px 15px;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(255, 118, 117, 0.3);
}

/* 添加按钮特殊样式 */
.add-btn {
  margin-top: 10px;
}

/* 统一“铭牌描述”输入为与“贡献比例(%)”完全一致的样式 */
.badge-form .badge-desc-input,
.badge-form > input:not([type="hidden"]),
.badge-form view > input:not([type="hidden"]),
.badge-form input[type="text"],
.badge-form input[type="number"] {
  width: 100% !important;
  padding: 14px 18px !important;
  border: 1px solid #ddd !important;
  border-radius: 16px !important;
  font-size: 16px !important;
  background: #ffffff !important;
  box-sizing: border-box !important;
  margin: 8px 0 12px !important;
  line-height: 1.5 !important;
  letter-spacing: 0.1px !important;
  min-height: 50px !important;
  color: #2c3e50 !important;
  -webkit-text-fill-color: #2c3e50 !important;
  caret-color: #2c3e50 !important;
  -webkit-appearance: none !important;
  appearance: none !important;
  color-scheme: light !important;
}
</style>
<style scoped>
/* 工作记录区块 */
.work-records {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.work-section-title {
  font-size: 16px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 12px;
}

.work-item {
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fafafa;
}

.work-item-row {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}

.work-item-row:last-child {
  margin-bottom: 0;
}

.work-item-label {
  min-width: 48px;
  color: #34495e;
  font-weight: 600;
  margin-right: 12px;
}

.work-item-value {
  flex: 1;
  color: #2c3e50;
}

.empty-records {
  color: #7f8c8d;
}

/* 工作操作按钮区块 */
.work-actions {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.start-work-btn {
  background: #0984e3;
}

.end-work-btn {
  background: #e17055;
}

/* 任务操作按钮区域 */
.task-actions {
  width: calc(100vw - 64px);
  margin: 0 auto 16px auto;
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cancel-apply-btn {
  background: #ff7675;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.request-cancel-btn {
  background: #fdcb6e;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.delete-task-btn {
  background: #e17055;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  border-bottom: 1px solid #e2e8f0;
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  color: #2c3e50;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #7f8c8d;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  display: flex;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
}

.modal-cancel-btn {
  flex: 1;
  background: #95a5a6;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.modal-confirm-btn {
  flex: 1;
  background: #74b9ff;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.modal-confirm-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.cancel-reason-textarea {
  min-height: 100px;
  width: 100%;
  padding: 14px 18px;
  border: 1px solid #ddd;
  border-radius: 12px;
  font-size: 16px;
  background: #ffffff;
  box-sizing: border-box;
  margin: 8px 0;
  resize: none;
  transition: all 0.3s ease;
  line-height: 1.5;
  color: #2c3e50;
  -webkit-text-fill-color: #2c3e50;
  caret-color: #2c3e50;
  -webkit-appearance: none;
  appearance: none;
  color-scheme: light;
}

.cancel-reason-textarea:focus {
  border-color: #6c5ce7;
  outline: none;
  box-shadow: 0 0 0 2px rgba(108, 92, 231, 0.1);
  background: #ffffff;
  color: #2c3e50;
  -webkit-text-fill-color: #2c3e50;
}

/* 待处理消息样式 */
.pending-messages {
  background: #fff3cd;
  border: 1px solid #ffeaa7;
  border-radius: 12px;
  padding: 16px;
  margin-top: 12px;
}

.pending-title {
  font-size: 16px;
  font-weight: 700;
  color: #856404;
  margin-bottom: 8px;
  display: block;
}

.pending-item {
  margin-bottom: 4px;
}

.pending-item text {
  font-size: 14px;
  color: #856404;
  font-weight: 500;
}

.view-messages-btn {
  background: #fdcb6e;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 12px;
  width: 100%;
}

.view-messages-btn:hover {
  background: #e17055;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}


.preview-proof {
  background: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px;
  margin-top: 8px;
  font-size: 14px;
  line-height: 1.5;
  color: #2c3e50;
  min-height: 60px;
}

.badge-header {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 12px;
  padding: 8px 0;
  border-bottom: 1px solid #e2e8f0;
}

.contributors-list {
  margin-bottom: 20px;
}

.contributor-item {
  margin-bottom: 12px;
}

.contributor-info {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.contributor-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
  border: 2px solid #e2e8f0;
}

.contributor-details {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.contributor-name {
  font-size: 14px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 2px;
}

.contributor-role {
  font-size: 12px;
  color: #7f8c8d;
}

.contributor-percentage {
  font-size: 16px;
  font-weight: 700;
  color: #74b9ff;
}

.contributor-avatar-small {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 8px;
  object-fit: cover;
  border: 1px solid #e2e8f0;
}

.preview-field {
  background: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  color: #2c3e50;
  min-height: 20px;
  display: flex;
  align-items: center;
}

.preview-actions {
  display: flex;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
  background: #f8f9fa;
}

.reject-btn {
  flex: 1;
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.reject-btn:hover {
  background: #c0392b;
  box-shadow: 0 4px 12px rgba(231, 76, 60, 0.3);
}

.approve-btn {
  flex: 1;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.approve-btn:hover {
  background: #229954;
  box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
}

/* 申请完成详情预览样式（复制申请完成样式） */
.apply-complete-preview-section {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  margin: 16px;
  border: 1px solid #e2e8f0;
}

/* 预览字段样式 */
.preview-proof {
  background: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px;
  margin-top: 8px;
  min-height: 60px;
  font-size: 14px;
  color: #2c3e50;
  line-height: 1.5;
}

.preview-field {
  background: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  color: #2c3e50;
  min-height: 20px;
  display: flex;
  align-items: center;
}

/* 申请完成处理按钮样式（弹窗内） */
.completion-actions {
  display: flex;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
  background: #f8f9fa;
  position: sticky;
  bottom: 0;
  z-index: 10;
}

/* 申请完成处理按钮样式（页面底部） */
.completion-actions-bottom {
  display: flex;
  gap: 12px;
  padding: 20px;
  background: #f8f9fa;
  border-top: 1px solid #e2e8f0;
  position: sticky;
  bottom: 0;
  z-index: 100;
}

.reject-completion-btn {
  flex: 1;
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.reject-completion-btn:hover {
  background: #c0392b;
  box-shadow: 0 4px 12px rgba(231, 76, 60, 0.3);
}

.approve-completion-btn {
  flex: 1;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.approve-completion-btn:hover {
  background: #229954;
  box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
}

/* 申请方式选择 */
.application-methods {
  margin-top: 16px;
}

.individual-btn {
  flex: 1;
  background: #4A90E2;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.individual-btn:hover {
  background: #357ABD;
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.3);
}

.team-btn {
  flex: 1;
  background: #9B59B6;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.team-btn:hover {
  background: #8E44AD;
  box-shadow: 0 4px 12px rgba(155, 89, 182, 0.3);
}

/* 申请模式选择样式 */
.application-mode-selection {
  margin-bottom: 16px;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  display: inline-block;
  width: auto;
  max-width: 200px;
}

.mode-option {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.mode-checkbox {
  width: 18px;
  height: 18px;
}

.mode-label {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

/* 组队申请表单样式 */
.team-application-form {
  margin-top: 16px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.team-info-section, .team-members-section, .pending-invitations-section {
  margin-bottom: 16px;
  padding: 12px;
  background: #fff;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

/* 解散团队按钮样式 */
.dissolve-team-section {
  margin-top: 16px;
  text-align: center;
}

.dissolve-team-btn {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
  transition: all 0.3s ease;
}

.dissolve-team-btn:active {
  transform: translateY(1px);
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.4);
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
  display: block;
}

.team-info-item {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-label {
  font-size: 14px;
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.info-value {
  font-size: 14px;
  color: #333;
}

.team-name-edit {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-top: 6px;
}

.team-name-input {
  flex: 1;
  padding: 10px 12px !important;
  border: 1px solid #ddd !important;
  border-radius: 6px !important;
  font-size: 14px !important;
  min-height: 36px !important;
  width: 100% !important;
  box-sizing: border-box !important;
  background: #ffffff !important;
  color: #2c3e50 !important;
  -webkit-text-fill-color: #2c3e50 !important;
  caret-color: #2c3e50 !important;
  -webkit-appearance: none !important;
  appearance: none !important;
  color-scheme: light !important;
}

.save-name-btn {
  padding: 10px 16px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.3s ease;
  min-width: 60px;
  white-space: nowrap;
  height: 36px;
}

.save-name-btn:hover:not(:disabled) {
  background: #45a049;
}

.save-name-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 成员列表样式 */
.members-list, .invitations-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.member-item, .invitation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  background: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.member-avatar, .invitation-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.member-info, .invitation-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.member-name, .invitation-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.member-role {
  font-size: 12px;
  color: #666;
}

.member-status {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 500;
}

.status-0 {
  background: #fff3cd;
  color: #856404;
}

.status-1 {
  background: #d4edda;
  color: #155724;
}

.status-2 {
  background: #f8d7da;
  color: #721c24;
}

.status-3 {
  background: #d1ecf1;
  color: #0c5460;
}

.invitation-time {
  font-size: 12px;
  color: #999;
}

/* 成员操作按钮样式 */
.member-actions {
  display: flex;
  gap: 8px;
}

.remove-member-btn, .leave-team-btn {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.remove-member-btn {
  background: #dc3545;
  color: white;
}

.remove-member-btn:hover {
  background: #c82333;
}

.leave-team-btn {
  background: #6c757d;
  color: white;
}

.leave-team-btn:hover {
  background: #5a6268;
}

.vote-remove-btn {
  background: #fa8c16;
  color: white;
}

.vote-remove-btn:hover {
  background: #d46b08;
}

.impeach-leader-btn {
  background: #f5222d;
  color: white;
}

.impeach-leader-btn:hover {
  background: #cf1322;
}

/* 好友搜索区域样式 */
.friend-search-section {
  margin-top: 8px;
}

.search-input-group {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.search-input {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
}

.search-btn {
  background: #4A90E2;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}

.search-loading, .search-error, .search-empty, .search-placeholder {
  text-align: center;
  color: #666;
  padding: 20px;
}

.search-error {
  color: #e74c3c;
}

.search-results {
  max-height: 300px;
  overflow-y: auto;
}

.search-result-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.2s;
}

.search-result-item:hover {
  background: #f8f9fa;
}

.search-result-item.already-selected {
  background: #e8f5e8;
  color: #2e7d32;
}

.search-friend-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
}

.search-friend-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.friend-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}

.search-friend-name {
  font-size: 15px;
  font-weight: 500;
  color: #333;
}

.friend-status-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 500;
}

.friend-status-badge.accepted {
  background: #e8f5e8;
  color: #2e7d32;
}

.friend-status-badge.pending {
  background: #fff3e0;
  color: #f57c00;
}

.friend-status-badge.not_friend {
  background: #f3e5f5;
  color: #7b1fa2;
}

.status-text {
  font-size: 10px;
  font-weight: 500;
}

.search-friend-uid {
  font-size: 12px;
  color: #666;
}

.search-result-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-end;
}

.invite-btn, .add-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.invite-btn {
  background: #4CAF50;
  color: white;
}

.invite-btn:hover {
  background: #45a049;
}

.add-btn {
  background: #2196F3;
  color: white;
}

.add-btn:hover {
  background: #1976D2;
}

.invited-status, .added-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 10px;
  text-align: center;
}

.invited-status {
  background: #e8f5e8;
  color: #2e7d32;
}

.added-status {
  background: #e3f2fd;
  color: #1976d2;
}

.invited-text, .added-text {
  font-size: 10px;
  font-weight: 500;
}


/* 团队名称显示样式 */
.team-name-display {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.team-name-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.role-hint {
  font-size: 12px;
  color: #999;
}

/* 队员提示样式 */
.member-notice {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
  margin-top: 10px;
}

.notice-text {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

.notice-hint {
  font-size: 14px;
  color: #999;
  margin-top: 4px;
}

/* 申请按钮样式 */
.apply-btn {
  background: #6c5ce7;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
  margin-top: 20px;
}

.apply-btn:hover:not(:disabled) {
  background: #5a4fcf;
  box-shadow: 0 4px 12px rgba(108, 92, 231, 0.3);
}

.apply-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style>